首页 > 解决方案 > 如何在 laravel eloquent 的多个表中使用多个 where?

问题描述

我想通过使用 laravel eloquent 来获得相同的结果。

我的数据库结构是-

我需要通过 branch.id 获取用户配置文件,其中 user.subusertype 是“员工”。

我有这个查询构建器代码现在我需要用 laravel eloquent 来制作它。

            $employeedata=DB::table('branch')
                     ->join('profile','profile.branch_id',"=",'branch.id')
                     ->join('user','profile.user_id','=','user.id')
                     ->join('company','profile.company_id','=','company.id')
             ->select('profile.*','user.email','user.subusertype','company.name','branch.address')
                    ->where('branch.id',$branch_id)
                    ->where('user.subusertype',"Employee")
                    ->get();

我试过这段代码,但它返回所有配置文件,这些子用户类型不是“员工”,给用户对象空值。

标签: laraveleloquent

解决方案


使用可以用作:

$employeedata=Branch::select('profile.*','user.email','user.subusertype','company.name','branch.address')
                     ->join('profile','profile.branch_id',"=",'branch.id')
                     ->join('user','profile.user_id','=','user.id')
                     ->join('company','profile.company_id','=','company.id')
                     ->where('branch.id',$branch_id)
                     ->where('user.subusertype',"Employee")
                     ->get();

您可以使用 laravel 而不是使用连接Eloquent: Relationships。要了解 laravel 关系,请单击此处


推荐阅读