检查count()函数中的多行

时间:2013-10-12 00:59:46

标签: php mysql laravel laravel-4

不确定我是否遗漏了一些简单的东西(我已经在这方面工作了一段时间)。我正在尝试制作一本简单的约会书,我有一个简单的表格,里面有时间段

id timeslot montgomery birmingham
1   8-10    2        2
2   9-12    6        3
3   12-3    6        3
4   3-5     2        2

我有这个作为我的控制器

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');
}

除了这一行以外,一切顺利:

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

它正在做的是计算整个一天,而不是检查时间段是否也在进行:

日程表的结构

id   date   timeslot
1   2013-10-11  1
1   2013-10-11  1
1   2013-10-11  4

因此,如果您尝试在10-11上预订8-10的时间段,那么您将无法获得,因为它已经完整......这很好,但您也无法预订3-5,因为它的身份也是2。我如何检查并从那里继续前进?

1 个答案:

答案 0 :(得分:2)

添加第二个where子句:

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

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