Menu

Laravel Searchable additional conditions

Written by

I’m using the Laravel Searchable plugin for a couple of months and it’s working fine. As a new task in one of my projects, I have to add additional conditions to the search.

So, my idea is if a user is blocked (it has status == 0) it not be displayed in the search result.

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 a 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 this in my controller

Article Categories:
Laravel · Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *

Shares