将mysql查询转换为laravel查询

时间:2016-10-18 01:59:14

标签: mysql laravel-5

如何将mysql查询转换为laravel查询? 这是查询

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

1 个答案:

答案 0 :(得分:0)

试试这样:

$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();