【问题标题】:How to get users based on multiple whereHas Condition Laravel如何根据多个 whereHas 条件获取用户 Laravel
【发布时间】:2021-01-08 08:44:12
【问题描述】:

我正在尝试根据多个 whereHas 条件来获取用户。首先,我得到具有特定角色的用户,然后我想对具有其他 whereHas 条件的用户进行排序。我尝试了一些 sn-ps 但我得到了所有具有不同角色的用户。

我的模特

  public function document()
    {
        return $this->hasOne(UserDocument::class,'user_id');
    }
    public function portfolios()
    {
        return $this->hasMany(Portfolio::class,'user_id');
    }

控制器

 public function index()
    {
        $data = User::whereHas("roles", function($q)
        { 
            $q->where("name", "Seller");
        })
        ->whereHas('portfolios',function($q){
            $q->where('status','Pending');
        })
        ->orWhereHas('document',function($s){
            $s->where('status','Pending');
        })
        ->get();
        
        dd($data);
        return view('admin.pendingapproval.index');
    }

我不明白如何做到这一点。请帮帮我

谢谢

【问题讨论】:

    标签: php mysql laravel


    【解决方案1】:

    您在 where has 中使用“或”,因此如果用户具有卖方角色,您将获得该用户 其投资组合状态为待处理其文档状态为待处理

    你应该这样做:

      $data = User::whereHas("roles", function ($q) {
            $q->where("name", "Seller");
        })
            ->where(function ($query) {
                $query->whereHas('portfolios', function ($q) {
                    $q->where('status', 'Pending');
                })->orWhereHas('document', function ($s) {
                    $s->where('status', 'Pending');
                });
            })->get();
    

    现在您可以获得具有“卖家”角色的用户 并且他有待处理的投资组合待处理的文档

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 2018-10-03
      • 2019-12-28
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2019-07-16
      • 2021-07-01
      • 1970-01-01
      相关资源
      最近更新 更多