shureban/laravel-searcher
Composer 安装命令:
composer require shureban/laravel-searcher
包简介
Laravel SDK for searching data in DB using requests
README 文档
README
Installation
Require this package with composer using the following command:
composer require shureban/laravel-searcher
Add the following class to the providers array in config/app.php:
Shureban\LaravelSearcher\SearcherServiceProvider::class,
You can also publish the config file to change implementations (i.e. interface to specific class).
php artisan vendor:publish --provider="Shureban\LaravelSearcher\SearcherServiceProvider"
How to use
By default, place your searchers in app\Http\Searchers folder.
Example with all searcher variations.
getQuery method
Should return query for applying filters. You may modify that
query Model::query()->with(['relation_1', 'relation_2' => fn() => ...])
getFilters method
Should return associative array where:
- keys must be equal to your request params
- values must be objects related of
\Shureban\LaravelSearcher\Searcher
Example
In that case:
- 'age' - is request parameter age
- 'client_age' - is DB column name
- new Between(...) - filter rule
return ['age' => new Between('client_age')];
class YourFirstSearcher extends Searcher { /** * @return Builder */ protected function getQuery(): Builder { return Model::query(); } /** * @return ColumnFilter[] */ protected function getFilters(): array { return [ //| Request param name | Filter object | Expected value type //------------------------------------------------------------------------- // Simple cases 'is_single' => new Boolean('is_single'), // bool 'age' => new Between('client_age'), // array (2 elements) 'salary' => new BetweenRange('salary'), // array (2 elements) 'birthday' => new BetweenDates('birthday'), // array (2 elements) 'id' => new Equal('id'), // any 'created_at' => new EqualDate('created_at'), // date 'height' => new Gt('height'), // number 'max_height' => new Gte('max_height'), // number 'updated_at' => new GtDate('updated_at'), // date 'deleted_at' => new GteDate('deleted_at'), // date 'statuses' => new In('status'), // array 'image_id' => new IsNull('image_id'), // bool 'email' => new Like('email'), // mixed 'foot_size' => new Lt('foot_size'), // number 'max_foot_size' => new Lte('max_foot_size'), // number 'birthday' => new LtDate('birthday'), // date 'hired_at' => new LteDate('hired_at'), // date 'partner_statuses' => new NotIn('partner_status'), // array 'only_every_even' => new Callback( fn(Builder $query, mixed $value) => $query->whereRaw('(id % 2 = 0)') ), // mixed // Modifier used. That case means, all rows where manager_id is equal to same value or null 'manager_id' => new OrNull(new Like('manager_id')), 'full_name' => new OrEmpty(new Like('full_name')), 'owner_id' => new MultipleOr(new Equal('user_id'), new Like('manager_id'), new Relation('brokers', new Equal('id'))), // Working with relation modifiers 'invoice_payouts' => new Relation('invoices', new Between('amount')), 'invoice_statuses' => new Relation('invoices', new In('status')), 'invoice_payment_method' => new Relation('invoices', new Like('payment_method')), 'invoice_process_statuses' => new Relation('invoices', new NotIn('process_status')), ]; } }
How to change Sorting
For change default sort column, you should override method sortColumn.
protected function sortColumn(): ?string { return $this->request->get('sort_column', 'created_at'); }
If you need to change order behavior related with some column, do this. Override method applySortBy
protected function applySortBy(Builder $query, string $sortColumn, SortType $sortType): Builder { return match ($sortColumn) { 'your_special_column' => $query->orderBy('column_1', $sortType)->orderBy('column_2', $sortType), default => parent::applySortBy($query, $sortColumn, $sortType), }; }
Real case
Your first searcher
namespace App\Http\Searchers; use Illuminate\Database\Eloquent\Builder; use Shureban\LaravelSearcher\Filters\Like; use Shureban\LaravelSearcher\Searcher; class YourFirstSearcher extends Searcher { /** * @return Builder */ protected function getQuery(): Builder { return Model::query(); } /** * @return ColumnFilter[] */ protected function getFilters(): array { return [ 'id' => new Like('id'), ]; } }
Request
namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rules\Enum; use Illuminate\Validation\Rules\In; use Shureban\LaravelSearcher\Enums\SortType; class YourRequest extends FormRequest { /** * Get the validation rules that apply to the request. * * @return array */ public function rules(): array { return [ 'id' => ['int', 'min:1', 'max:2000000000'], 'sort_column' => [new In(['id'])], 'sort_type' => [new Enum(SortType::class)], ]; } }
Controller
namespace App\Http\Controllers; use Illuminate\Routing\Controller as BaseController; use App\Http\Requests\YourFirstSearcher; class YourController extends BaseController { /** * @param YourRequest $request * * @return JsonResponse */ public function __invoke(YourRequest $request): JsonResponse { $searcher = new YourFirstSearcher($request); // Collection with all models without pagination and slicing by per_page $allModels = $searcher->all(); // Collection contains per_page number of models and with page offset $models = $searcher->get(); // Base Laravel LengthAwarePaginator $paginator = $searcher->paginate(); return new JsonResponse($paginator); } }
shureban/laravel-searcher 适用场景与选型建议
shureban/laravel-searcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.7k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 05 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「requests」 「models」 「Searcher」 「shureban」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 shureban/laravel-searcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 shureban/laravel-searcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 shureban/laravel-searcher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Rest client to make GET, POST, PUT, DELETE, PATCH, etc calls.
A Laravel package to retrieve data from Google Search Console
Wordpress Query Builder class library for custom models and data querying.
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
Abstraction Layer to index and search entities
This package is free and can be used for API Brazil website functions
统计信息
- 总下载量: 5.7k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-22