DEV Community

StuartCreed
StuartCreed

Posted on

Using SQL bindings in Laravel Raw Methods

Raw methods are sometimes required when there is not an eloquent relationship available.

? Binding

Used a lot in the Laravel Raw methods e.g.

User::whereRaw('name = ?', ['Stuart'] )->get()

Here the ? the SQL query gets replace with the String 'Stuart'

Named bindings

User::whereRaw('name = :name', [':name' => 'Stuart'] )->get()

Here the :name the SQL query gets replaced with the String 'Stuart'. Any name can be used

Top comments (0)