Home > Software design >  Laravel Query Builder bindings
Laravel Query Builder bindings

Time:03-10

Can someone please explain why in Laravel the first code works while the second one doesn't work.

$data = DB::select("SELECT * FROM people WHERE name LIKE '%john%'");


$data = DB::select("SELECT * FROM people WHERE name LIKE '?'", ['%john%']);

CodePudding user response:

If you what to use Query Builder you can do this:

$data = DB::select("SELECT * FROM people WHERE name LIKE :name", ['name' => "%john%"]);

But it is better to use Eloquent:

People::where('name', 'like', "%john%")->get();
  • Related