下午好,我想请求你的帮助。
请帮我将此查询应用到Laravel查询生成器中。我只知道如何使用原始查询查询数据库。
查询:
SELECT users.first_name, users.last_name, users.id as userID, bookmarks.id,
products.title, products.price, products.id AS productID FROM users,
products, bookmarks WHERE bookmarks.user_id = $user_id AND products.id =
bookmarks.product_id AND users.id = products.user_id;
感谢那些能帮助我的人。
答案 0 :(得分:1)
Use this Laravel Query.
$response = DB::table('users as u')->select(
'u.id as userId',
'u.first_name',
'u.last_name',
'b.id as bookmarkId',
'p.id as productId',`enter code here`
'p.title',
'p.price'
)
->leftjoin('bookmarks as b', 'b.user_id', '=', 'u.id')
->leftjoin('products as p', 'p.user_id', '=', 'u.id')
->where('u.id', $userId)->get();
答案 1 :(得分:0)
$result = DB::select(DB::raw('custom_query'));
$user_name = $result[0]->first_name.' '.$result[0]->last_name;
答案 2 :(得分:0)
将userss.first_name,users.last_name,users.id选为userID, bookmarks.id,products.title,products.price,products.id AS productID FROM用户,产品,书签WHERE bookmarks.user_id = $ user_id AND products.id = bookmarks.product_id AND users.id = products.user_id;
您可以使用eloquent with方法,只需访问关系,但如果您想要laravel查询方法。您可以使用DB :: raw来拥有自己的原始查询。
DB::select(DB::raw("Select a.first_name, a.last_name, a.id as user_id, b.id, c.title, c.price, c.id from users as a INNER JOIN bookmarks as b on b.user_id = $user_id INNER JOIN Products as c on c.id = b.product_id where user_id = c.user_id"));
这只是一个示例,它仍然取决于您如何操纵查询。
爱 Poop