Mat*_*ijn 3 php laravel eloquent
是否可以“追加”查询Laravel?
例如在这个示例代码中这样:
function crunchNumbersForToday()
{
return $this->crunchSomeInformationInRange(Payments::where('created_at', Carbon::now()));
}
function crunchNumbersInRange($range)
{
$paymentsToCrunch = Payment::where('state', 'payed')->append($range)->get();
// Work with the payments
}
Run Code Online (Sandbox Code Playgroud)
因此,在这种情况下where,created_at字段的 the将附加到查询 where stateequals 中payed。这将使类似:where created_at = '2015-07-08 12:00' AND state = payed。
我认为你可以创建一个scope.
对于您的代码(在您的Payment模型中):
function scopeToday($query)
{
return $query->where('created_at', Carbon::now());
}
function scopeNumbersInRange($query)
{
return $query->where('state', 'payed');
// Work with the payments
}
Run Code Online (Sandbox Code Playgroud)
然后在代码中的其他地方调用它,例如:
Payment::numbersInRange()->today()->get();
Run Code Online (Sandbox Code Playgroud)
编辑:您也可以使它们动态化:
function scopeDate($query, $date)
{
return $query->where('created_at', $date);
}
Run Code Online (Sandbox Code Playgroud)
...
Payment::numersInRange()->date(Carbon::now())->get();
Run Code Online (Sandbox Code Playgroud)
等等。通过这种方式,您可以保持链接范围。
| 归档时间: |
|
| 查看次数: |
5700 次 |
| 最近记录: |