How to do a search with multiple filters as a parameter in the Laravel?

Asked

Viewed 3,313 times

2

I’m having a question about how to do a customer search with multiple filters.

Example: nome cliente, tipo de cliente, status do cliente, endereço, cidade.

Detail: all these filters are in different fields.

How to do this using the Eloquent of Laravel?

  • I’ve never worked with Laravel, but have you tried anything? If so, put the code of what you tried. You have already looked at the documentation http://laravel.com/docs/eloquent . And from what I looked at, if I understood it, it would be something like $query->where('campo', $valor)->where('outroCampo', $outroValor)

  • Place the tables or tables that make up this Model, including the Model class, please, this way imagining that you will not have any answer or until you have some that comes closer to your reality ...

  • What do you mean "are in different fields"? do you want to make a query based on html form filters? If it is, I already have a quick answer :)

1 answer

3

Analyzing the documentation, there in http://laravel.com/docs/queries#Advanced-wheres
has this example that will suit you very well:

MeuModel::where(function($query)
        {
            $query->where('votes', '>', 100)
                  ->orWhere('title', '<>', 'Admin');
        })
        ->get();

Ai you can mount dynamically what you want, nesting as many "orWhere" you need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.