Laravel5如何向闭合函数内传递参数 where function 传参

如上,怎么将$title 像$query一样,在函数内部使用?

$result = UserMenus::with(['menu'=>function($query){
$query->where('title',$title);
}])->where('user_id',$userId)->first();

 

解决:

$result = UserMenus::with(['menu'=>function($query) use ($title){
$query->where('title',$title);
}])->where('user_id',$userId)->first();


或者
$result = UserMenus::with(function($query) use ($title){
$query->where('title',$title);
})->where('user_id',$userId)->first();

 

 

 

 

posted on 2019-11-28 10:27  潇潇六月雨  阅读(1721)  评论(0编辑  收藏  举报

导航