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'));
}
这是我得到的Undefined变量的代码:screening。我想问题是$screening在另一个函数中。我该如何解决?
您可以使用use
:使闭包中的变量可用
$seats = DB::table('seats')->whereNotIn('id', function($q) use ($screening) {
$q->select('seat_id')->from('reservations')
->where('screening_id', $screening->id);
})->get();
有关匿名函数/闭包的详细信息:http://php.net/manual/en/functions.anonymous.php-您特别感兴趣的是">示例#3从父作用域继承变量">