dongqingcheng2903 2017-10-14 05:13
浏览 666
已采纳

Laravel Eloquent在WhereHas中无效

I'm trying to display all business locations hours for the current day and if the location does not have hours listed in the database, I'm wanting to show it but as null.

Example:

Business 1: 10:00 am - 5:00 pm
Business 2: 10:00 am - 9:00 pm
Business 3: Closed (null)
Business 4: 10:00 am - 5:00 pm

The issue i'm having is in the controller. It's not showing the nulls with the whereHas clause.

$Locations = Locations::with('StoreHours', 'Managers', 'ShiftLeads')
                ->doesntHave('StoreHours')->orWhereHas('StoreHours', function ($query) {
                    $query->where('opening_time','>=', date('Y-m-d') . ' 00:00:00');
                    $query->where('opening_time','<=', date('Y-m-d') . ' 23:59:59');
                })
            ->get();

The only data that is showing is if any store has never had store hours. How can I show all stores and get null if there is no hours?

  • 写回答

1条回答 默认 最新

  • doudou8893 2017-10-14 05:31
    关注

    The issue with your query is that locations that have store hours for any day other than today will not be selected.

    It seems to me that you want all locations, regardless of whether they have store hours or not. Just remove the restrictions on the store hours relationship from the query. That will give you all your locations, and then your view can determine if there are store hours to display for today.

    Edit

    From your comment, I assume what you really want to do is to restrict the store hours that are eager loaded to only those hours for today, so that you're not loading a bunch of store hours you don't care about. You can do that by tweaking the parameters passed to your with() method. Your code would look something like:

    $locations = Locations::with([
            'Managers',
            'ShiftLeads',
            'StoreHours' => function ($query) {
                return $query
                    ->where('opening_time', '>=', date('Y-m-d') . ' 00:00:00')
                    ->where('opening_time', '<=', date('Y-m-d') . ' 23:59:59');
            }
        ])
        ->get();
    

    Now, only those store hours that match the closure will be eager loaded onto each location. You can read more about constraining eager loading in the documentation here.

    Another option would be to create a second relationship that only loads the store hours for today, and eager load that relationship instead of the full store hours relationship. If you're only doing this in one place, the extra work may not be worth it, but you may want to consider this if you need this logic all over the place.

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘