如何将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

最佳答案

尝试这样:

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

08-04 03:51