DEV Community

loading...

Using SQL bindings in Laravel Raw Methods

stuartcreed profile image StuartCreed ・1 min read

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

Discussion (0)

Forem Open with the Forem app