【问题标题】:laravel5.1(eloquent) order & extend returned collectionlaravel5.1(eloquent) 订购 & 扩展返回的集合
【发布时间】:2016-04-08 07:38:59
【问题描述】:

我有一个使用这种设计的事务表:

|id|value|payed_out|...|created_at|updated_at

现在我想退回所有按日期排序且限制为 15 个的交易:

  • not payed_out(表示 payed_out == null)
  • 不早于 24 小时(表示从现在到“created_at”的时间戳为

查询可能类似于:

Transactions::where(payed_out, null)->where(?how to perform the not older check?) ->orderBy('created_at', 'desc') ->take(15)
           ->get();

我需要“扩展”上述查询的返回集合。

在我看来,我想显示交易“过期”之前的时间,这意味着我需要将一个字段“添加”到包含交易“created_at”字段中的“时间”的集合中。

希望你能理解我的需要。

【问题讨论】:

    标签: sql eloquent laravel-5.1 where-clause


    【解决方案1】:

    您可以尝试使用Carbon 减去当前时间戳。见Carbon Addition and Subtraction。字段 created_at 必须大于此时间戳才能被视为不早于 1 天。例如:

    Transactions::where('payed_out', null)->
              where('created_at', '>', \Carbon\Carbon::now()->subDay())->
              orderBy('created_at', 'desc') ->take(15)->get();
    

    【讨论】:

      猜你喜欢
      • 2016-07-23
      • 1970-01-01
      • 2014-04-02
      • 2013-08-25
      • 2011-05-17
      • 1970-01-01
      • 2014-07-14
      • 2012-01-08
      • 1970-01-01
      相关资源
      最近更新 更多