protonemedia / laravel-cross-eloquent-search

Laravel package to search through multiple Eloquent models. Supports sorting, pagination, scoped queries, eager load relationships and searching through single or multiple columns.
https://protone.media/blog/search-through-multiple-eloquent-models-with-our-latest-laravel-package
MIT License
1.1k stars 80 forks source link

how to filter deeply in an eloquent in users model #59

Closed constantinosergiou closed 2 years ago

constantinosergiou commented 2 years ago

e.g

 $query = Search::new()
            ->add(User::select('*')->with('Role')->access($request)
            ->selectRaw('(SELECT roles.name FROM roles, role_users
                           WHERE users.id = role_users.user_id
                            AND role_users.role_id = roles.id LIMIT 1)
                           as rolename')
            ->groupBy('id'), ['first_name', 'last_name'])
            ->paginate(\Request::get('per_page') ?: 30)->search($request['query']);

i want to filter roles also so when i am doing this

      if ($request['roles']) {
              $query->orwhereHas('Role', function ($q) use ($request) {
                    $q->whereIn('role_id', $request['roles']);
                });
            }

i get this error

    "message": "Method Illuminate\\Database\\Eloquent\\Collection::orwhereHas does not exist.",