Skip to content

Instantly share code, notes, and snippets.

@m5lil
Last active January 25, 2019 20:57
Show Gist options
  • Save m5lil/e94b4530fb2f1220e91e554ada08e212 to your computer and use it in GitHub Desktop.
Save m5lil/e94b4530fb2f1220e91e554ada08e212 to your computer and use it in GitHub Desktop.
laravel queryable

I'd suggest something like this:

url/item/?registered=>:DDMMYYYY The parameter name is the name of the attribute Right at the beginning of the parameter value is the operator Operator and value is separated by a : (it actually can be any separation character you want) Other examples:

url/item/?name=like:foo
url/item/?email==:foo.bar@example.com

I agree email==:foo looks a bit weird. You could also use words or abbreviations ("eq", "gt", etc) instead of operator signs.

How to parse it

$filters = Input::all();
$query = Model::newQuery();
foreach($filters as $attribute => $filter){
    $parts = explode(':', $filter, 2);
    $operator = $parts[0];
    $value = $parts[1];
    $query->where($attribute, $operator, $value);
}

I hope this gives you an idea how you could do it ;)

reference : https://stackoverflow.com/questions/27594543/query-strings-and-laravel-with-inequalities

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