This package makes it easy to get structured search from a variety of sources. Here's an example where we search through some models. We already did some small preparation on the models themselves.
$searchResults = (new Search())
->registerModel(User::class, 'name')
->registerModel(BlogPost::class, 'title')
->search('john');
The search will be performed case insensitive. $searchResults
now contains all User
models that contain john
in the name
attribute and BlogPost
s that contain 'john' in the title
attribute.
In your view you can now loop over the search results:
<h1>Search</h1>
There are {{ $searchResults->count() }} results.
@foreach($searchResults->groupByType() as $type => $modelSearchResults)
<h2>{{ $type }}</h2>
@foreach($modelSearchResults as $searchResult)
<ul>
<li><a href="{{ $searchResult->url }}">{{ $searchResult->title }}</a></li>
</ul>
@endforeach
@endforeach
In this example we used models, but you can easily add a search aspect for an external API, list of files or an array of values.
This package now includes a recent search feature that allows tracking, retrieving, and managing user searches efficiently.
-
Install Package Cia Composer
Run the following command to install the package
composer require tonymans33/laravel-searchable-with-recent-search
-
Publish Config and Migration Files
Run the following command to publish the configuration and migration files:
php artisan vendor:publish --provider="Tonymans33\SearchableWithRecent\Providers\RecentSearchServiceProvider"
-
Run Migrations
After publishing the migration files, migrate your database:
php artisan migrate
-
Update the Config File
In the
config/recentsearch.php
file, set theuser_model
to your User model:'user_model' => App\Models\User::class,
To enable recent search functionality for a model, add the HasRecentSearchTrait
to it:
use HasRecentSearchTrait;
-
Store a Recent Search
User::storeRecentSearch($request->q);
This saves the recent search query for the user.
-
Retrieve Recent Searches
$recentSearches = User::getRecentSearches();
This retrieves all recent searches for the user.
-
Delete a Specific Search Record
User::deleteRecentSearchRecord($id);
This deletes a specific search record by its ID.
-
Clear All Recent Searches
User::clearRecentSearches();
This clears all recent search records for the user.
In order to search through models you'll have to let them implement the Searchable
interface.
namespace Tonymans33\SearchableWithRecent;
interface Searchable
{
public function getSearchResult(): SearchResult;
}
You'll only need to add a getSearchResult
method to each searchable model that must return an instance of SearchResult
. Here's how it could look like for a blog post model.
use Tonymans33\SearchableWithRecent\Searchable;
use Tonymans33\SearchableWithRecent\SearchResult;
class BlogPost extends Model implements Searchable
{
public function getSearchResult(): SearchResult
{
$url = route('blogPost.show', $this->slug);
return new \Tonymans33\SearchableWithRecent\SearchResult(
$this,
$this->title,
$url
);
}
}
The MIT License (MIT). Please see License File for more information.