Heim >Backend-Entwicklung >PHP-Tutorial >Laravel5如何向闭合函数内传递参数

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 20:30:521291Durchsuche

如上,怎么将$title 像$query一样,在函数内部使用?
$result = UserMenus::with(['menu'=>function($query){
$query->where('title',$title);
}])->where('user_id',$userId)->first();

回复内容:

如上,怎么将$title 像$query一样,在函数内部使用?
$result = UserMenus::with(['menu'=>function($query){
$query->where('title',$title);
}])->where('user_id',$userId)->first();

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

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

话说PHP 有全局函数,你加global 不行吗?

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn