tonymans33/laravel-searchable-with-recent-search
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
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.
tonymans33/laravel-searchable-with-recent-search 适用场景与选型建议
tonymans33/laravel-searchable-with-recent-search 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 146 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 01 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel-searchable-with-recent-search」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tonymans33/laravel-searchable-with-recent-search 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tonymans33/laravel-searchable-with-recent-search 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 146
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-22