php-Laravel雄辩:追加查询

是否可以在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
}

因此,在这种情况下,created_at字段的where将附加到状态等于payed的查询中.内容如下:where created_at =’2015-07-08 12:00’AND state =已付款.

解决方法:

我认为您可以创建一个scope.

对于您的代码(在您的付款模型中):

function scopetoday($query)
{
    return $query->where('created_at', Carbon::Now());
}

function scopeNumbersInRange($query)
{
    return $query->where('state', 'payed');

    // Work with the payments
}

然后在代码中的其他地方调用它,例如:

Payment::numbersInRange()->today()->get();

编辑:
您也可以使它们动态化:

function scopeDate($query, $date)
{
    return $query->where('created_at', $date);
}

Payment::numersInRange()->date(Carbon::Now())->get();

等等.这样,您可以保持链接范围.

相关文章

laravel的dd函数不生效怎么办
看不懂laravel文档咋办
安装laravel框架出现command怎么办
Laravel开发API怎么使用事务
laravel怎么构建复杂查询条件
laravel如何实现防止被下载