ahmetbarut/laravel-full-search
Composer 安装命令:
composer require ahmetbarut/laravel-full-search
包简介
Allows searching on multiple models. The advantage of this for us; a separate search field is not required for each model.
README 文档
README
Allows searching on multiple models. The advantage of this for us; a separate search field is not required for each model.
Features
- Mutli-model search
- Search on multiple fields
- definition of routes
Installation
composer require ahmetbarut/laravel-full-search
Configuration
All configurations related to the model are made from the config/fullsearch.php file.
published config
php artisan vendor:publish --provider="AhmetBarut\\LaravelFullSearch\\Providers\\LaravelFullSearchServiceProvider" --tag="fullsearch-config"
view side
The @stack('scripts') directive must be defined on the view side, otherwise it will not work.
<body> + <x-fullsearch /> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"> </script> + @stack('scripts') </body> </html>
If you are using macos, you can open the search window with the cmd + K key combination.
Usage
You need to configure the config/fullsearch.php file for use.
// config/fullsearch.php use App\Models\Post; return [ 'models' => [ Post::class => [ 'searchable' => true, 'ability' => false, // false or middleware name example: auth => 'auth', 'searchable_fields' => [ 'title', 'body', 'id', ], 'response_parameters' => [ 'title' => 'title', 'page' => 'Posts', ], 'route' => [ 'name' => 'post', 'parameters' => [ 'post' => 'id', ], ], 'max_results' => 10, ], ], ];
In this way, you can search the title, id and body fields on the Post model. You can set the number of search results with the max_results parameter.
searchable => if true then search is possible.
ability => if false then search is possible. If you want to restrict some models for certain users, you need to use Laravel Gate. I will explain it below.
searchable_fields => specifies searchable fields.
response_parameters => specifies the parameters to be displayed in the search result.
route => specifies the routes to show in the search result.
max_results => specifies the number of search results.
Restrict Specific Users
// app/Providers/AuthServiceProvider.php use Illuminate\Support\Facades\Gate; public function boot() { $this->registerPolicies(); Gate::define('admin', function ($user) { return $user->isAdmin(); }); }
// config/fullsearch.php use App\Models\User; return [ 'models' => [ User::class => [ 'searchable' => true, 'ability' => 'admin', 'searchable_fields' => [ 'name', 'email', 'id', ], 'response_parameters' => [ 'name' => 'name', 'page' => 'Users', ], 'route' => [ 'name' => 'user', 'parameters' => [ 'user' => 'id', ], ], 'max_results' => 10, ], ], ];
Users must have 'admin' authority to search on them. If you do not have admin privileges, you cannot search. It does not return an error message that it cannot search, it just returns an empty array.
Let's talk about response_parameters and route['parameters']. The response_parameters parameter specifies the parameters to be displayed in the search result. The route['parameters'] parameter specifies the routes to be displayed in the search result. If you have noticed now, the name column has come across the name parameter in the response_parameters, so the name column will be shown as a return. If we want, we can email it.
// config/fullsearch.php 'searchable_fields' => [ 'name', 'email', 'id', ], 'response_parameters' => [ - 'name' => 'name', + 'name' => 'email', 'page' => 'Users', ],
The column to be displayed must be defined in
searchable_fields.
Now let's deepen on route['parameters']. Similar usage exists in response_parameters.
... // config/fullsearch.php 'searchable_fields' => [ 'name', 'email', 'id', ], 'route' => [ 'name' => 'user', 'parameters' => [ - 'user' => 'id', + 'user' => 'email', ], ], ...
It will be like this /user/user@email.com.
ahmetbarut/laravel-full-search 适用场景与选型建议
ahmetbarut/laravel-full-search 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ahmetbarut/laravel-full-search 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ahmetbarut/laravel-full-search 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-04-24