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 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计