tonymans33 / laravel-searchable-with-recent-search
Pragmatically search through models and other sources, with a recent search feature. build on top of Spatie Searchable.
                                    Fund package maintenance!
                                                                            
                                                                                                                                        spatie.be/open-source/support-us
                                                                                    
                                                                
Installs: 124
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 111
pkg:composer/tonymans33/laravel-searchable-with-recent-search
Requires
- php: ^7.3|^8.0
- ext-pdo: *
- laravel/framework: ^8.78|^9.0|^10.0|^11.0
Requires (Dev)
- larapack/dd: ^1.0
- orchestra/testbench: ^6.27|^7.0|^8.0
- phpunit/phpunit: ^9.3|^10.0
README
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 BlogPosts 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.
Recent Search Feature
About
This package now includes a recent search feature that allows tracking, retrieving, and managing user searches efficiently.
Installation
- 
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.phpfile, set theuser_modelto your User model:'user_model' => App\Models\User::class, 
Usage
Add the Trait
To enable recent search functionality for a model, add the HasRecentSearchTrait to it:
use HasRecentSearchTrait;
Available Functions
- 
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. 
Original Features
Preparing your models
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 ); } }
Credits
License
The MIT License (MIT). Please see License File for more information.