← PHP / Laravel · Contents

PHP-002 / Intermediate

User input in Eloquent whereRaw or DB::raw

A03:2021 Injection · CWE-89 SQL Injection

Why is this a problem?

Raw methods bypass binding, so concatenated strings become SQL directly.

How to fix it

php
User::where('name', 'LIKE', '%'.addcslashes($request->q, '%_\\').'%')->get();

Further reading