montesilva/laravel-search-sort
Composer 安装命令:
composer require montesilva/laravel-search-sort
包简介
Eloquent model search and sort trait.
README 文档
README
LaravelSearchSort is a fork from nicolaslopezj/searchable. It's a trait for Laravel 7+ that adds a simple search, sort function to Eloquent Models.
LaravelSearchSort allows you to perform searches in a table giving priorities to each field for the table and it's relations, and also sorting these results by given columns and directions.
This is not optimized for big searches, but sometimes you just need to make it simple (Although it is not slow).
Installation
Simply add the package to your composer.json file and run composer update.
"montesilva/laravel-search-sort": "1.*"
You can also use this command:
composer require montesilva/laravel-search-sort
Laravel 8 Support
For Laravel 8 config/database.php must be changed.
In config/database.php, set mysql's 'strict' to false.
'mysql' => [ ... 'strict' => false, ... ],
Usage
Add the trait to your model and add your search and/or sort rules.
use Montesilva\LaravelSearchSort\SearchSortTrait; class User extends \Eloquent { use SearchSortTrait; /** * Searchable rules. * * @var array */ protected $search_sort = [ /** * Columns and their priority in search results. * Columns with higher values are more important. * Columns with equal values have equal importance. * * @var array */ 'search_columns' => [ 'users.first_name' => 10, 'users.last_name' => 10, 'users.bio' => 2, 'users.email' => 5, 'posts.title' => 2, 'posts.body' => 1, ], 'sort_columns' => [ 'users.first_name', 'users.last_name', 'posts.title', 'posts.email' ], 'joins' => [ 'posts' => ['users.id','posts.user_id'], ], 'groupBy' => [ 'posts.id' ] ]; public function posts() { return $this->hasMany('Post'); } }
Search
Now you can search your model.
// Simple search $users = User::search($query)->get(); // Search and get relations // It will not get the relations if you don't do this $users = User::search($query) ->with('posts') ->get();
Sort
Or you can sort your model.
// Sort array with sortable column and direction (asc, desc) $sorts = [ [ 'prop' => 'users.first_name', 'dir' => 'asc' ] ]; // Simple sort $users = User::sort($sorts)->get(); // Sort and get relations // It will not get the relations if you don't do this $users = User::sort($query) ->with('posts') ->get();
Search Paginated
As easy as laravel default queries
// Search with relations and paginate $users = User::search($query) ->with('posts') ->paginate(20);
Sort Paginated
// Sort array with sortable column and direction (asc, desc) $sorts = [ [ 'prop' => 'users.first_name', 'dir' => 'asc' ] ]; // Search with relations and paginate $users = User::sort($sorts) ->with('posts') ->paginate(20);
Search and Sort
You can also search and sort at the same time
// Sort array with sortable column and direction (asc, desc) $sorts = [ [ 'prop' => 'users.first_name', 'dir' => 'asc' ] ]; // Search and sort with relations and paginate $users = User::searchSort($query, $sorts) ->with('posts') ->paginate(20);
Mix queries
Search and Sort methods are compatible with any eloquent method. You can do things like this:
// Search only active users $users = User::where('status', 'active') ->search($query) ->paginate(20);
Mix queries with joins before search and sort
You can join other tables first to have the join tables available to include them in any eloquent method
// Add joins first $builder = User::addJoins()->where('posts.title', 'Test'); // Search and sort without making joins in scope $users = $builder::searchSort($query, $sorts, false) ->paginate(20);
// Search or sort without making joins in search or sort scope $users = User::addJoins() ->where('posts.title', 'Test') ->search($query, false) ->sort($sorts, false) ->with('posts') ->paginate(20);
Custom Threshold
The default threshold for accepted relevance is the sum of all attribute relevance divided by 4. To change this value you can pass in a second parameter to search() like so:
// Search with lower relevance threshold $users = User::where('status', 'active') ->search($query, true, 0) ->paginate(20);
The above, will return all users in order of relevance.
Entire Text search
By default, multi-word search terms are split and LaravelSearchSort searches for each word individually. Relevance plays a role in prioritizing matches that matched on multiple words. If you want to prioritize matches that include the multi-word search (thus, without splitting into words) you can enable full text search by setting the third value to true. Example:
// Prioritize matches containing "John Doe" above matches containing only "John" or "Doe". $users = User::search("John Doe", null, true, true)->get();
If you explicitly want to search for full text matches only, you can disable multi-word splitting by setting the fourth parameter to true.
// Do not include matches that only matched "John" OR "Doe". $users = User::search("John Doe", null, true, true, true)->get();
Entire Text search sorted
// Do not include matches that only matched "John" OR "Doe". $users = User::searchSort("John Doe", $sorts, null, true, true, true)->get();
Contributing
Anyone is welcome to contribute. Fork, make your changes, and then submit a pull request.
montesilva/laravel-search-sort 适用场景与选型建议
montesilva/laravel-search-sort 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 10 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「search」 「sortable」 「model」 「sort」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 montesilva/laravel-search-sort 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 montesilva/laravel-search-sort 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 montesilva/laravel-search-sort 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Easily provide front-end sorting controls for SilverStripe lists
A Laravel package to retrieve data from Google Search Console
Store your language lines in the database, yaml or other sources
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-10-08