illusi03/lara-search
Composer 安装命令:
composer require illusi03/lara-search
包简介
Advanced model and relationships search for Laravel
README 文档
README
LaraSearch is a search package for laravel that use recursivity to efficiently find a record of a model, by occurrences of any word found in the search and in any relationship given for the model.
Let's say you have a model Post, the model Post hasMany comments, and the model Comment belongsTo a User. Beside, your app user searched for the string "Galactic Empire,and... pizza for steve".
Yup, that makes sense. If you want to find a post given that string in any part of the relationship chain of the Post model it could, for example:
- bring a post which title is "The love for pizza".
- bring a post that has a comment that says "Wow that was out of this empire"
- bring a post that has a comment posted by a guy named "Steve". Hi Steve.
Installation
Require the package in your composer.json and update composer to download the package
composer require illusi03/lara-search
After that, add the ServiceProvider to the providers array in config/app.php
Illusi03\LaraSearch\ServiceProvider::class,
if you want to, add the facade for convenience
'LaraSearch' => Illusi03\LaraSearchFacade::class,
How to Use
Using the trait
Use the trait in your model
use Illusi03\LaraSearch\Traits\LaraSearchable; class Model extends Eloquent { use LaraSearchable; ... }
The LaraSearch() method will bring back your search results. It takes 3 arguments:
- $search the search string to find
- $fields an array with the fields of the model you want to search in
- $relationFields an associative array with the relations of the main model and their fields you want to search in
$posts = Post::laraSearch($userInput, ['title'], [ 'comments' => 'comment', 'comments.user' => ['name', 'lastname'] ])->get();
Using the static class
Alternatively you can use the static class LaraSearch::find(). It takes 3 arguments:
- $search the search string to find
- $model the model from which you want to return the records. ex: 'App\Post'
- $searchSchema an array with the relations of the main model you want to search in, as well as what fields. The format is as follows:
$searchSchema = [ 'fields' => ['title'], // Fields where you want to search in the main model 'relationships' => [ // Relationships, if any [ 'relationship' => 'comments', // Here you put name of the relationship 'fields' => 'comment', // And here the fields where you want to search in the related table ], [ 'relationship' => 'comments.user', // Use dot notation for inner relations 'fields' => ['name', 'lastname'], ] ] ];
Chaining
The LaraSearch() and find() methods return a query, so you need to bring the results by yourself using get() or paginate(n) and even chaining other methods. Following the example above we get:
$search = LaraSearch::find($userInput, App\Models\Post::query(), $searchSchema)->where('active', 1)->paginate(10);
Authors
- lHumanizado
- Rubenazo
- Illusi03
illusi03/lara-search 适用场景与选型建议
illusi03/lara-search 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 146 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 10 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 illusi03/lara-search 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 illusi03/lara-search 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 146
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-10-04