Skip to content

Instantly share code, notes, and snippets.

@franzliedke
Created March 1, 2016 12:52
Show Gist options
  • Save franzliedke/e351e4a4d4f7d3d481a4 to your computer and use it in GitHub Desktop.
Save franzliedke/e351e4a4d4f7d3d481a4 to your computer and use it in GitHub Desktop.
Laravel scope with binding
<?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)
}
}
@SteveEdson
Copy link

Additionally, if I mix a scope like the one above with another one as simple as return $query->where('search_type', 'proximity');, the mixed named and positional parameters error returns.

@franzliedke
Copy link
Author

Hmm, whereRaw accepts a second parameter called bindings: https://github.com/laravel/framework/blob/c858d541b1a05c3a13734c17f5d9e69fe6e1502b/src/Illuminate/Database/Query/Builder.php#L564

You should try that (with numeric indices and question marks as placeholders, yes).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment