首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在count()函数中检查多行

在count()函数中检查多行
EN

Stack Overflow用户
提问于 2013-10-12 00:59:46
回答 1查看 64关注 0票数 0

不知道我是不是错过了一些简单的东西(我已经做了很长一段时间了)。我正试着做一本简单的预约书,我有一个简单的桌子,里面有时隙。

代码语言:javascript
复制
id timeslot montgomery birmingham
1   8-10    2        2
2   9-12    6        3
3   12-3    6        3
4   3-5     2        2

我把这个当我的控制器

代码语言:javascript
复制
public function getSchedule() { 
    $user = User::find(Auth::user()->id);   

    $input = [
        'date' => Input::get('date'),
        'timeslot' => Input::get('timeslot')
    ];                  

    $date = strtotime($input['date']);

    $dateFormat = date('Y-m-d',$date);

    $block = DB::table('bk_timeslot')
            ->where('id', '=', $input['timeslot'])
            ->first();      

    if($user->office == "Birmingham") {

        $count = Schedule::where('date', '=', $dateFormat)->count();

        $full = "Sorry Birmingham does not have an appointment open for this day";

        if ($count >= $block->birmingham) {
            return Redirect::to('book/schedule')->withErrors($full)->withInput();
        }   
    }   

    if($user->office == "Montgomery") {


        $count = Schedule::where('date', '=', $dateFormat)          
                ->count();

        var_dump($count); die;

        $full = "Sorry Montgomery does not have an appointment open for this day";

        if ($count >= $block->montgomery) {
            return Redirect::to('book/schedule')->withErrors($full)->withInput();
        }   
    }       

    //puts info in a session for later use
    Session::put('schedule', $input); 

    return Redirect::to('book/review');
}

除了这一行之外,所有的功能都很好:

代码语言:javascript
复制
$count = Schedule::where('date', '=', $dateFormat)->count();

它所做的就是计算一整天,而不是检查时隙是否也在消耗:

进度表的结构

代码语言:javascript
复制
id   date   timeslot
1   2013-10-11  1
1   2013-10-11  1
1   2013-10-11  4

所以,如果你试图在10-11上预定一个8到10的时隙,你将无法做到,因为它已经满了。这很好,但你也不能预订3-5,因为它也是id 2。我怎样才能检查这两种情况,然后从那里出发呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-12 01:14:32

添加第二个where子句:

代码语言:javascript
复制
$count = Schedule::where('date', '=', $dateFormat)
         ->where('timeslot', '=', $block->id)
         ->count();

您可以使用$input['timeslot']而不是$block->id (它们应该是相同的)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19329330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档