Home > Backend Development > PHP Tutorial > Laravel5如何向闭合函数内传递参数

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:30:52
Original
1294 people have browsed it

如上,怎么将$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>
Copy after login

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

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

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
laravel5 namespace problem
From 1970-01-01 08:00:00
0
0
0
How do you use smarty to integrate laravel5?
From 1970-01-01 08:00:00
0
0
0
Merge requirejs in laravel5
From 1970-01-01 08:00:00
0
0
0
vendor - laravel5 problems after cloning from git
From 1970-01-01 08:00:00
0
0
0
laravel5 cannot start
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template