Search

Setup

https://github.com/spatie/laravel-searchable/

composer require spatie/laravel-searchable

Controller

public function search(Request $request)
{
$searchResults = (new \Spatie\Searchable\Search())
->registerModel(User::class, ['name'])
->search($request->q);
return view('admin.search', compact('searchResults'));
}

Route

Route::get('/search', 'DashboardController@search')->name('search');

View

@extends('layouts.admin')
@section('content')
<div class="text-2xl flex">
<div class="font-bold">Search Results for&nbsp;</div>
<div class="">{{ request('q') }}</div>
</div>
<div class="font-semibold text-gray-500 mt-4">
There are {{ $searchResults->count() }} results.
</div>
@foreach($searchResults->groupByType() as $type => $modelSearchResults)
<card title="{{ $type }}" class="mt-3">
<ul>
@foreach($modelSearchResults as $sr)
<wide-list-item url="{{ $sr->url }}">
{{ $sr->title }}
</wide-list-item>
@endforeach
</ul>
</card>
@endforeach
@endsection