【问题标题】:Laravel - Advanced Carbon Date QueryLaravel - 高级碳日期查询
【发布时间】:2016-12-13 06:14:38
【问题描述】:

我正在尝试编写某个查询,但我失败了,我正在寻求帮助

这就是我想做的事情

SELECT all items WHERE created_at is from before this month (July, June,...), and also 选择前 3 个在本月创建的

这是我目前拥有的。我已经尝试了很多次,但我无法找出正确的“WHERE”案例

   $offertes = DB::table('offertes')
      ->select('*')
      ->where('receiver_id', $user_id)
...
      ->orderby('seen')
      ->orderby('created_at','desc')
      ->get();

【问题讨论】:

    标签: php mysql laravel php-carbon


    【解决方案1】:

    这样的事情应该可以工作:

    $time = new Carbon\Carbon('first day of this month'); // Get first day of the month.
    $time->setTime(0, 0, 0); // Set time to 00:00:00.
    ....
    ->where('created_at', '<', $time)
    ->where(function($q) {
        $q->where('created_at', '>', $time)
          ->orderby('created_at','asc') 
          ->take(3)
    })
    ->get;
    

    【讨论】:

    • 那你把当月的前 3 个拿去哪里呢?我编辑了 OP 使其更清晰
    猜你喜欢
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 2017-08-25
    • 2018-03-16
    • 2016-08-09
    • 2016-03-18
    • 2021-08-16
    • 2012-12-18
    相关资源
    最近更新 更多