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

Posted 潇潇六月雨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();

 

 

 

 

以上是关于Laravel5如何向闭合函数内传递参数 where function 传参的主要内容,如果未能解决你的问题,请参考以下文章