【问题标题】:Laravel Query: BadMethodCallException in Builder.php line 2258:Laravel 查询:Builder.php 第 2258 行中的 BadMethodCallException:
【发布时间】:2016-05-14 06:12:51
【问题描述】:

我正在构建一个 laravel 应用程序,在控制器中我正在检查时间重叠问题。

我使用的逻辑首先在我的控制器的查询中,我将检查作为输入给出的 day_id 是否与具有该 day_id 的数据库匹配,然后它将检查时间,如果匹配则不能让用户保存输入,否则如果查询失败,它将让用户保存数据。

public function postAllocateRoom(Request $request)
    {


            $startTime = Carbon::parse(str_replace(array('am', 'pm'), ':00', $request->input('start')));
            $endTime = Carbon::parse(str_replace(array('am', 'pm'), ':00', $request->input('end')));
            $dayId = $request->input('day_id');

            $timeExists = ClassRoom::where('day_id', $dayId)
                                                ->andWhere('start', $startTime)
                                                ->andWhere('end', $endTime)
                                                ->exists();

            if($timeExists){
                return redirect('allocateRoomPage')->withErrors(['time' => 'Class Room Already Taken']);
            }

            $classRoom = new ClassRoom();  
            $classRoom->department_id=$request->input('department_id');     
            $classRoom->room_id=$request->input('room_id'); 
            $classRoom->course_id=$request->input('course_id'); 
            $classRoom->day_id=$dayId;
            $classRoom->start=$startTime;
            $classRoom->end=$endTime;
            $classRoom->save();

            $request->session()->flash('success', 'Successfully allocated room');

        return redirect('allocateRoomPage'); 


}  

但运行程序后,我看到以下错误:

Builder.php 第 2258 行中的 BadMethodCallException:调用未定义 方法 Illuminate\Database\Query\Builder::andWhere()

如果有人发现问题,请帮助我找到解决方案。

【问题讨论】:

    标签: php mysql sql laravel laravel-5


    【解决方案1】:

    简单使用

     $timeExists = ClassRoom::where('day_id', $dayId)
                                                ->Where('start', $startTime)
                                                ->Where('end', $endTime)
                                                ->exists();
    

    因为 laravel 中没有 andWhere 方法

    【讨论】:

      【解决方案2】:

      没有andWhere 方法。
      简单的where(<...>)->where(<...>) 就像where <...> and where <...>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-13
        • 2016-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-17
        相关资源
        最近更新 更多