douan5151 2016-12-21 18:46
浏览 23
已采纳

Laravel Eloquent计算价值

I have a column in my table called date which is of type TIMESTAMP.

I am trying to add a very simply scope which will return all results that have a date of tomorrow.

I think I want to be able to do something like this:

public function scopeTomorrow($query)
{
    return $query->where('date', function ($date) {
        return \Carbon\Carbon::parse($date)->isTomorrow();
    });
}

But the $date variable is currently just the query builder.

How can I create a where statement which accepts the value and perfroms some sort of check on it?

  • 写回答

2条回答 默认 最新

  • duanfuxing2212 2016-12-21 18:52
    关注

    You can do this:

    return $query->where('date', '>=', Carbon::tomorrow())
                 ->where('date', '<=', Carbon::tomorrow()->endOfDay());
    

    Or:

    return $query->whereBetween('date', [Carbon::tomorrow(), Carbon::tomorrow()->endOfDay()]);
    

    Also, probably you want to add date to $dates variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示