cleaniquecoders / global-search
Laravel Scout 的简化全局搜索
1.3.0
2024-03-21 04:08 UTC
Requires
- php: ^8.1 | ^8.2 | ^8.3
- illuminate/contracts: ^10.0 | ^11.0
- laravel/scout: ^10.0 | ^11.0
- spatie/laravel-enum: ^3.0 | ^4.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.4
- orchestra/testbench: ^8.0 | ^9.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.3
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
README
为您的应用程序提供简化的全局搜索 URL 端点。
安装
您需要先安装 Laravel Scout,然后选择您希望使用的驱动程序
然后您可以通过 composer 安装此软件包
composer require cleaniquecoders/global-search
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="global-search-config"
用法
配置您的模型,使其 可搜索。
接下来,在您的 routes/api.php
中添加以下内容
use CleaniqueCoders\GlobalSearch\GlobalSearch; GlobalSearch::routes();
现在您可以在前端使用 API 搜索路由
axios.get('search', { 'type' => 'user', 'keyword' => 'nasrul' }) .then(...)
如果您需要在应用程序内部调用,可以从后端使用 search()
辅助函数
// get the first result search(\App\Models\User::class, 'nasrul'); // get all possible result by passing true to the third argument // this will return a paginated result search(\App\Models\User::class, 'nasrul', true);
要添加更多搜索功能,您可以在 app/Enums/SearchType
类中添加更多枚举值。
<?php namespace App\Enums; use Spatie\Enum\Laravel\Enum; /** * @method static self user() * @method static self profile() */ class SearchType extends Enum { public static function values(): array { return [ 'user' => \App\Models\User::class, 'profile' => \App\Models\Profile::class, ]; } protected static function labels(): array { return [ 'user' => __('User'), 'profile' => __('Profile'), ]; } }
测试
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
贡献
有关详细信息,请参阅 CONTRIBUTING。
安全漏洞
有关如何报告安全漏洞的详细信息,请参阅 我们的安全策略。
致谢
许可协议
MIT 许可协议 (MIT)。有关更多信息,请参阅 许可文件。