定制 tonymans33/laravel-searchable-with-recent-search 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tonymans33/laravel-searchable-with-recent-search

最新稳定版本:v1.0.7

Composer 安装命令:

composer require tonymans33/laravel-searchable-with-recent-search

包简介

Pragmatically search through models and other sources, with a recent search feature. build on top of Spatie Searchable.

README 文档

README

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.

统计信息

  • 总下载量: 129
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 110
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-22