Skip to content

tonymans33/laravel-searchable-with-recent-search

 
 

Repository files navigation

Laravel Searchable with Recent Search Feature

Latest Version on Packagist run-tests Total Downloads

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

  1. Install Package Cia Composer

    Run the following command to install the package

    composer require tonymans33/laravel-searchable-with-recent-search
  2. 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"
  3. Run Migrations

    After publishing the migration files, migrate your database:

    php artisan migrate
  4. Update the Config File

    In the config/recentsearch.php file, set the user_model to 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.

About

Package build on laravel searchable with recent searches features

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%