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();

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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...