douli2876 2015-05-11 11:35
浏览 144
已采纳

无法比较Laravel 5中的Carbon实例?

I am working with the fullcalendar plugin and I am trying to fetch all the events in my database after and before a certain date. Full calendar paginates the events by adding paramaters called 'start' and 'end'. I am able to parse these in to Carbon instances like so:

Carbon::parse($data['start']);
Carbon::parse($data['end']);

Now this is working fine:

$events = $this->event->where('user_id', auth()->user()->id)
                        ->get();

But this is not:

$events = $this->event->where('user_id', auth()->user()->id)
                        ->where('start', '>=', Carbon::parse($data['start']))
                        ->where('end', '<=', Carbon::parse($data['end']))
                        ->get();

Why am I not getting events published between these two dates with the second approach. When I run the first approach, it works fine and fetches all the events and publishes them on the current page but unfortunately it is also fetching a whole lot of events that don't need fetching.

After listening to the DB queries, the event query was like:

select * from events where user_id = ? and start >= ? and end <= ?

  • 写回答

1条回答 默认 最新

  • douyi7283 2015-05-12 07:59
    关注

    This was a small mistake on my end where I was not setting the end date for the event in my DB and hence the query where all the events for the authenticated users was working correctly.

    In fact the query for >= start would also have worked alone but the the <= end was creating as issue since end was null and hence Eloquent was not returning any models.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口