Converting mysql query to laravel query

Matiullah Karimi

How I can convert mysql query to laravel query? this is the query

SELECT p.*, 
    IF(p.id = wish.product_id,1,0) AS status
FROM products as p LEFT JOIN `wishlists` as wish ON wish.product_id = p.id and product_id AND wish.user_id = 1
Filip Koblański

Try like this:

$result = \DB::table('products as p')
    ->select(\DB::raw('IF(p.id = wish.product_id,1,0) AS status'))
    ->leftJoin('wishlists as wish', function ($join) {
        $join->on('wish.product_id', '=', 'p.id and product_id')
        $join->on('wish.user_id', '=', \DB::raw(1))
    })
    ->get();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related