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

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?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试显示当天的所有营业地点营业时间以及地点是否列出了营业时间 数据库,我想显示它但是为空。

示例:

 商业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 
   
 \  n 

我遇到的问题是在控制器中。 它没有使用 whereHas 子句显示空值。

  $ Locations = Locations :: with('StoreHours','Managers','ShiftLeads')
  - &gt; doesntHave('StoreHours') - &gt; orWhereHas(  'StoreHours',函数($ query){
 $ query-&gt; where('opening_time','&gt; =',date('Ym-d')。'00:00:00'); 
 $  query-&gt; where('opening_time','&lt; =',date('Ym-d')。'23:59:59'); 
})
  - &gt; get(); 
 <  / code>  
 
 

显示的唯一数据是任何商店从未有过商店营业时间。 如果没有小时,我如何显示所有商店并获得空?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题