dongmo8943 2017-11-13 03:16
浏览 45

Wheredoesnt在Laravel中有关系

I've a problem doing a query using Eloquent/Laravel.

This is my model: A request has ZERO or multiple offers. An offer can be hide (it's an attribute set on the offer).

What I need to do is to retrieve all requests that either have or don't have offers. BUT, if there's an offer associated, the offer needs to have the attribute hide=false.

I've tried the following, but doesn't seem to work.

$requests = Request::whereDoesntHave('offers', function ($query) use ($hideStatus) {
   $query->where('hide', true);
})->get();

I think it's returning to me only requests that don't have offers at all.

Any idea?

FYI, I've Laravel 5.4

Any help would be greatly appreciated!

Thanks

  • 写回答

2条回答 默认 最新

  • dsdzz04802 2017-11-13 03:37
    关注

    Check below query. I didn't test the query, wrote it with the knowledge I have.

    $requests = Request::whereHas('offers', function ($query) {
                        $query->where('hide', false);
                    })
                    ->orDoesntHave('offers')
                    ->get();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用