Search code examples
jquerysqllaravelmodels

A query to search on the whole model [Laravel]


I want some query to search on all of [User] Model for example !!

That I can give it an input and search for it on all User's columns ..

is there some thing like that ?


Solution

  • Well you could add a where like for every column in the table:

    $columns = Schema::getColumnListing('users');
    $query = User::query();
    $search = Input::get('search');
    foreach($columns as $column){
        $query->orWhere($column, 'like', '%'.$search.'%');
    }
    $users = $query->get();