未定义变量:mysql whereNotIn

未定义变量:mysql whereNotIn,mysql,laravel,Mysql,Laravel,这是代码,我得到了一个未定义的变量:屏蔽。我想问题是$screening在另一个函数中。 如何解决此问题?您可以使用use使变量在闭包中可用: public function show($id) { $screening = Screnning::findOrFail($id); $seats = DB::table('seats')->whereNotIn('id',function($q){ $q->select('seat_id')

这是代码,我得到了一个未定义的变量:屏蔽。我想问题是$screening在另一个函数中。
如何解决此问题?

您可以使用
use
使变量在闭包中可用:

    public function show($id)
{
    $screening = Screnning::findOrFail($id);


    $seats = DB::table('seats')->whereNotIn('id',function($q){

        $q->select('seat_id')->from('reservations')
           ->where('screening_id',$screening) ;
          })->get();


    return view('reservation.show',compact('screening','seats'));

}

有关匿名函数/闭包的更多信息:-您特别感兴趣的是“示例#3从父范围继承变量”

您可以使用
使用
使闭包中的变量可用:

    public function show($id)
{
    $screening = Screnning::findOrFail($id);


    $seats = DB::table('seats')->whereNotIn('id',function($q){

        $q->select('seat_id')->from('reservations')
           ->where('screening_id',$screening) ;
          })->get();


    return view('reservation.show',compact('screening','seats'));

}

关于匿名函数/闭包的更多信息:-您特别感兴趣的是“示例3从父作用域继承变量”

$screening
看起来是一个模型,但可能希望
$screening->id
$screening
看起来是一个模型,可能需要
$screening->id
。您应该研究使用关系查询。您应该研究使用关系查询。