[英]Laravel: How Can I Convert This Sql Statement to Eloquent or Query Builder
How can I convert this Sql Statement to Eloquent or Query Builder 如何将此Sql语句转换为Eloquent或Query Builder
I have 4 tables; 我有4张桌子;
Jobs
Stores
Users
Store_User
Each Store hasMany Jobs while Stores and Users have a ManyToMany relationship through store_user table. 每个商店都有许多作业,而商店和用户通过store_user表具有ManyToMany关系。 I want to fetch all jobs of the stores a user is assigned to.
我想获取分配给用户的商店的所有作业。
Users are assigned to one or more stores through the store_user
table. 通过
store_user
表将用户分配到一个或多个商店。 And multiple jobs can belongTo a store. 并且多个作业可以属于一个商店。
I need to be able to use a $user->jobs
or something that can fetch the list of jobs belonging to the user based on the stores he/she is assigned to. 我需要能够使用
$user->jobs
或可以根据分配给他/她的商店获取属于用户的作业列表的东西。
Here is the SQL Statement. 这是SQL语句。 I was using in PDO.
我在PDO中使用。
SELECT * FROM jobs
INNER JOIN store_user
INNER JOIN stores
WHERE stores.id = store_user.store_id
AND store_user.store_id = jobs.store_id
AND store_user.user_id = 2
You could try this. 你可以试试看。
$jobs = DB::table('jobs')
->join('store_user','store_user.store_id','=','jobs.store_id')
->join('stores','stores.id','=','store_user.store_id')
->where('store_user.user_id', '=' ,2)
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.