How to transform this raw SQL query into Laravel Eloquent:)
SELECT
a_tables.`id` AS `a_id`,
b_tables.`id` AS `b_id`,
c_tables.`id` AS `c_id`
FROM
laravel.a_tables AS P
LEFT JOIN laravel.b_tables AS L ON L.`a_tables_id` = `a_id` AND L.user_id=2
LEFT JOIN laravel.c_tables AS I ON I.`a_tables_id` = `a_id` AND I.`b_tables_id` = `b_id` AND I.user_id=2
WHERE
P.user_id = 2;
try this
DB::table("laravel.a_tables as p")
->leftJoin("laravel.b_tables as l", function($join){
$join->on("l.a_tables_id", "=", "p.id")
->where("l.user_id", "=", 2);
})
->leftJoin("laravel.c_tables as i", function($join){
$join->on("i.a_tables_id", "=", "p.id")
->where("i.b_tables_id", "=", "l.id")
->where("i.user_id", "=", 2);
})
->select("p.id as a_id", "l.id as b_id", "i.id as c_id")
->where("p.user_id", "=", 2)
->get();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.