php Laravel Advanced Wheres 如何将变量传递给函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29548073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Advanced Wheres how to pass variable into function?
提问by frenzy
Example in doc:
文档中的示例:
DB::table('users')
->whereExists(function($query)
{
$query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get();
But what if I need to use external variable like that:
但是如果我需要像这样使用外部变量怎么办:
->where('city_id', '=', $this->city->id)
->where(function($query)
{
$query->where('name', 'LIKE', '%'.$searchQuery.'%')
->orWhere('address', 'LIKE', '%'.$searchQuery.'%')
})
For now I created new property and accessed it through $this->
, but is there any more convenient way?
现在我创建了新属性并通过 访问它$this->
,但有没有更方便的方法?
回答by kajetons
You can pass the necessary variables from the parent scope into the closure with the use
keyword.
您可以使用use
关键字将父作用域中的必要变量传递到闭包中。
For example:
例如:
DB::table('users')->where(function ($query) use ($activated) {
$query->where('activated', '=', $activated);
})->get();
More on that here.
更多关于这里。
EDIT (2019 update):
编辑(2019 年更新):
PHP 7.4 (will be released at November 28, 2019) introduces a shorter variation of the anonymous functions called arrow functionswhich makes this a bit less verbose.
PHP 7.4(将于2019 年 11 月 28 日发布)引入了一个更短的匿名函数变体,称为箭头函数,这使它变得不那么冗长。
An example using PHP 7.4 which is functionally nearlyequivalent (see the 3rd bullet point below):
一个使用 PHP 7.4 的示例,它在功能上几乎等效(请参阅下面的第三个要点):
DB::table('users')->where(fn($query) => $query->where('activated', '=', $activated))->get();
Differences compared to the regular syntax:
与常规语法相比的差异:
fn
keyword instead offunction
.- No need to explicitly list all variables which should be captured from the parent scope - this is now done automatically by-value. See the lack of
use
keyword in the latter example. - Arrow functions alwaysreturn a value. This also means that it's impossible to use
void
return type when declaring them. - The
return
keyword mustbe omitted. - Arrow functions musthave a single expression which is the return statement. Multi-line functions aren't supported at the moment. You can still chain methods though.
fn
关键字而不是function
.- 无需显式列出应从父作用域捕获的所有变量 - 现在按值自动完成。请参阅
use
后面的示例中缺少关键字。 - 箭头函数总是返回一个值。这也意味着
void
在声明它们时不可能使用返回类型。 - 该
return
关键字必须被省略。 - 箭头函数必须有一个表达式,即返回语句。目前不支持多行函数。不过,您仍然可以链接方法。
回答by Nagibaba
@kajetons' answer is fully functional.
@kajetons 的回答功能齐全。
You can also pass multiple variables by passing them like: use($var1, $var2)
您还可以通过传递多个变量来传递它们,例如: use($var1, $var2)
DB::table('users')->where(function ($query) use ($activated,$var2) {
$query->where('activated', '=', $activated);
$query->where('var2', '>', $var2);
})->get();
回答by Nikunj K.
If you are using Laravel eloquent you may try this as well.
如果你正在使用 Laravel eloquent 你也可以试试这个。
$result = self::select('*')
->with('user')
->where('subscriptionPlan', function($query) use($activated){
$query->where('activated', '=', $roleId);
})
->get();
回答by Ajay Singh
You can pass variables using this...
您可以使用此传递变量...
$status =1;
$info = JOBS::where(function($query) use ($status){
$query->where('status',$status);
})->get();
print_r($info);