I'm using the Laravel Searchable plugin for couple of months and it's working fine. As a new task in one of my projects, I have to add additional condition to the search.
So, my idea is if a user is blocked (it has status == 0) to not be displayed in the search result.
This is my code:
PageController:
public function searchMember(Request $request, $id)
{
$this->validate($request, [
'query' => 'required',
]);
$query = $request->input('query');
$searchResults = (new Search())
->registerModel(User::class, 'first_name', 'last_name')
->perform($query);
return view('user.search', compact('searchResults'));
}
View:
<div class="result-count">{{ $searchResults->count() }} results found for "{{ request('query') }}"</div>
<div class="result">
@foreach($searchResults->groupByType() as $type => $modelSearchResults)
@foreach($modelSearchResults as $searchResult)
<div class="article">
<a href="{{ $searchResult->url }}">{{ $searchResult->title }}</a>
</div>
@endforeach
@endforeach
</div>
It works fine but I want to add where() parameter, so I can display only active users (status 1). I've read if I want to add conditions, I have to tweak the code and I've tried like this in my controller:
public function searchMember(Request $request, $id)
{
$this->validate($request, [
'query' => 'required',
]);
$searchResults = (new Search())
->registerModel(User::class, function(ModelSearchAspect $modelSearchAspect) {
$modelSearchAspect
->addSearchableAttribute('first_name')
->addSearchableAttribute('last_name')
->where('status', 0); // This won't work
})->search($request->input('query'));
return view('user.search', compact('searchResults'));
}
but I'm getting this error:
Type error: Argument 1 passed to App\Http\Controllers\PageController::App\Http\Controllers\{closure}() must be an instance of App\Http\Controllers\ModelSearchAspect, instance of Spatie\Searchable\ModelSearchAspect given, called in...
Please help me to solve this issue.
EDIT:
OK, I've corrected the error by adding
use Spatie\Searchable\ModelSearchAspect;
on top of the controller.
How can I add condition into the searchResult?
Try type-hinting in closure
$searchResults = (new Search())
->registerModel(User::class, function(\App\Http\Controllers\ModelSearchAspect $modelSearchAspect) {
//...
})->search($request->input('query'));
I struggled with this one as well, but finally figured it out with scopes ( https://laravel.com/docs/8.x/eloquent#query-scopes ). You can read more about them in the Laravel docs, but here is working code that leverages them (user and organization scopes).I am searching across 4 models and each model has the scope where I can pass in the variables from an object that I have set ($user).
$this->results = (new Search()) ->registerModel(\App\Models\Meeting::class, function (ModelSearchAspect $modelSearchAspect) { $modelSearchAspect ->user($this->user->id) ->organization($this->user->organization_id) ->addSearchableAttribute('subject'); }) ->registerModel(\App\Models\Note::class, function (ModelSearchAspect $modelSearchAspect) { $modelSearchAspect ->user($this->user->id) ->organization($this->user->organization_id) ->addSearchableAttribute('note_title') ->addSearchableAttribute('note_content'); }) ->registerModel(\App\Models\Document::class, function (ModelSearchAspect $modelSearchAspect) { $modelSearchAspect ->user($this->user->id) ->organization($this->user->organization_id) ->addSearchableAttribute('document_note'); }) ->registerModel(\App\Models\Task::class, function (ModelSearchAspect $modelSearchAspect) { $modelSearchAspect ->user($this->user->id) ->organization($this->user->organization_id) ->addSearchableAttribute('task'); }) ->perform($searchterm);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.