如何在$data
Laravel
public function store(Request $request)
{
$data = [1,2,3,4];
echo User::whereHas('books', function($query){
$query->whereIn('id', $data);
})->get();
}
上面的代码显示错误
ErrorException thrown with message "Undefined variable: data"
答案 0 :(得分:1)
您需要add use($data)
to the closure:
User::whereHas('books', function($query) use($data) {
$query->whereIn('id', $data);
})->get();
答案 1 :(得分:1)
您可以使用use
public function store(Request $request)
{
$data = [1,2,3,4];
echo User::whereHas('books', function($query) use ($data){
$query->whereIn('id', $data);
})->get();
}
希望这有帮助