laravel - 如何在 laravel eloquent 的多个表中使用多个 where?
问题描述
我想通过使用 laravel eloquent 来获得相同的结果。
我的数据库结构是-
user
表:id、电子邮件、密码、user_role、子用户类型profile
表:id、名字、姓氏、电话、user_id、company_id、branch_idcompany
表:ID,名称branch
表:身份证、地址、联系方式
我需要通过 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();
我试过这段代码,但它返回所有配置文件,这些子用户类型不是“员工”,给用户对象空值。
解决方案
使用可以用作:
$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 关系,请单击此处
推荐阅读
- javascript - 如何将按钮文本更改为数组中的数字
- unity3d - Unity 2D 摄像机在角色移动时放大,但摄像机的属性不会改变
- powershell - Windows:通过 Powershell 脚本(远程)删除特定的 Active Directory 帐户
- javascript - Power BI Rest API - JS - 以编程方式设置视觉级别过滤器错误
- c++ - 为什么 MonitoringManager::OnRequestSucceeded() 中的 assert(s_monitors) 会失败?
- gradle - IntelliJ Kotlin multiplatform project Gradle sync very long
- javascript - Javascript - 悬停时播放视频的问题
- android - 使用 Readium 2 for android 的 Webview 错误
- reactjs - 我应该使用 ASP.NET Core 2.2 MVC React.js 和 Redux 模板吗?
- dart - Column 中的 Flutter 定位小部件