How do I convert a similar MySQL query to Laravel Eloquent?

SELECT * FROM `books` WHERE books.type = 'rare' AND books.band - $value < $result

That’s how I think it should be

$books= DB::table('books')
                        ->where('type ','=', 'rare')
                        ->whereRaw('band - $value < $result')   // Syntax error

I don’t understand how to work with dynamic variables here.

Answer

Please try it:

$books= DB::table('books')
            ->where('type ', 'rare')
            ->whereRaw('band - ? < ?', [$value, $result])
            ->get();

Leave a Reply

Your email address will not be published. Required fields are marked *