Created
March 1, 2016 12:52
-
-
Save franzliedke/e351e4a4d4f7d3d481a4 to your computer and use it in GitHub Desktop.
Laravel scope with binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SomeModel extends Eloquent | |
{ | |
public function scopeSomething(Builder $query, $something) | |
{ | |
// This will properly set up a binding | |
return $query->where('something', $something); | |
} | |
public function scopeSomethingAndOr(Builder $query, $something, $connector) | |
{ | |
if ($connector == 'and') { | |
return $query->where('something', $something); | |
} else { | |
return $query->orWhere('something', $something); | |
} | |
// Really, you can shorten this to the following: | |
// return $query->where('something', '=', $something, $connector) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hmm,
whereRaw
accepts a second parameter called bindings: https://github.com/laravel/framework/blob/c858d541b1a05c3a13734c17f5d9e69fe6e1502b/src/Illuminate/Database/Query/Builder.php#L564You should try that (with numeric indices and question marks as placeholders, yes).