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 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私