Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

esmaeil1's avatar

“like” operatior and laravel SQL injection in join raw query

up vote 0 down vote favorite In laravel if i have a model FORM and table cars, is it safe to use raw query like this: Form::crossJoin(DB::raw('(SELECT * FROM cars WHERE cars.title like "%somewords%")cars'))->get(); In this case i used like operator for search. Is it vulnerable for sql injection? If yes, how make it safe?

0 likes
4 replies
bobbybouwmann's avatar

It is right now! You need to do it like so

$search = 'something';
$raw = DB::raw('(SELECT * FROM cars WHERE cars.title like :search) cars', ['search' => '%' . $search . '%');

Form::crossJoin($raw)->get();
esmaeil1's avatar

Thanks. it seems dosnt work. i used setBindings function.

bobbybouwmann's avatar

setBinding is fine as well. According to the documentation this should work fine!

Anyway, did this fix your problem?

esmaeil1's avatar

The above query lead to this error: General error: 2031

Please or to participate in this conversation.