doncadavona/eloquenturl
Composer 安装命令:
composer require doncadavona/eloquenturl
包简介
Search and filter Laravel Eloquent models by query parameters with ease.
README 文档
README
Eloquenturl automatically adds search and filtering for your Eloquent models using query strings.
Eloquenturl respects the $fillable and $hidden attributes of your Eloquent models to make sensible defaults, providing zero-configuration usage. For example:
return \Doncadavona\Eloquenturl\Eloquenturl::eloquenturled(\App\Models\User::class, $request);
By just passing the model and the request, the model is made searchable and filterable without you having to manage which attributes are queryable and without writing complex logic and database queries.
Eloquenturl is fast because it is built and executed as a single database query.
Read Eloquenturl: A Laravel Package to Simplify or Eliminate Query Building for URL Parameters.
Table of Contents
- I. Installation
- II. Usage
- III. Query Parameters
- IV. Change Log
- V. Testing
- VI. Contributing
- VII. Security
- VIII. Credits
- IX. License
I. Installation
Install using Composer:
composer require doncadavona/eloquenturl
II. Usage
Eloquenturl::eloquenturled()
Use Eloquenturl::eloquenturled() to quickly build and execute the database query based on the request query parameters. It returns the paginated entries of the given Eloquent model, based on the URL parameters and the model's $fillable and $hidden attributes. Just pass the model and the request:
$users = \Doncadavona\Eloquenturl\Eloquenturl::eloquenturled(User::class, request());
Eloquenturl::eloquenturl()
Use Eloquenturl::eloquenturl() when you need to add additional queries, such as eager-loading, or any other database queries available in Laravel Query Builder.
Here is an example UsersController with several examples.
<?php use App\Models\User; use App\Models\Article; use App\Http\Controllers\Controller; use Doncadavona\Eloquenturl\Eloquenturl; use Illuminate\Http\Request; class UsersController extends Controller { public function index(Request $request) { // It's this easy. $users = Eloquenturl::eloquenturled(User::class, $request); // Or, add your own sauce. $users = Eloquenturl::eloquenturl(User::class, $request) ->with(['roles', 'articles', 'comments']) ->paginate(); // Or, with select query and simplePaginate $users = Eloquenturl::eloquenturl(User::class, $request) ->select('name', 'description') ->simplePaginate($request->per_page); return $users; // Or, use any other Eloquent model. $articles = Eloquenturl::eloquenturl(Article::class, $request) ->with(['user', 'comments']) ->get(); return $articles; } }
Here are example URLs with query parameters:
http://localhost:8000/users?search=apple http://localhost:8000/users?order_by=created_at&order=desc http://localhost:8000/users?scopes[role]=admin&scopes[status]=active&company_id=3 http://localhost:8000/users?search=apple&search_by=first_name&order_by=created_at&order=desc&scopes[role]=admin&scopes[status]=active&company_id=3
III. Query Parameters
- page
- per_page
- search
- search_by
- order
- order_by
- scopes
- lt (less than)
- gt (greater than)
- lte (less than or equal)
- gte (greater than or equal)
- min (alias for gte)
- max (alias for lte)
page
/users?page=5
per_page
/per_page?page=100
search
/users?search=john
/users?search=john+doe
/users?search=john%20doe
search_by
/users?search=john&search_by=first_name
/users?search=john+doe&search_by=last_name
/users?search=johndoe&search_by=email
order
/users?order=desc
order_by
/users?order_by=age
scopes
/users?scopes[status]=active
/users?scopes[senior]
/users?scopes[admin]
lt (less than)
/users?lt[age]=18
gt (greater than)
/users?gt[age]=17
lte (less than or equal)
/users?lte[age]=18
gte (greater than or equal)
/users?gte[age]=60
min (alias for gte)
/users?min[age]=18
max (alias for lte)
/users?max[age]=18
When unknown parameters are in the request, they will be queried with WHERE Clause using equality condition. For example:
/users?active=true /users?status=suspended /users?country=PH /users?planet_number=3 /users?company_id=99
The equivalent database queries will be:
SELECT * FROM `users` WHERE `active` = true; SELECT * FROM `users` WHERE `status` = 'suspended'; SELECT * FROM `users` WHERE `country` = 'PH'; SELECT * FROM `users` WHERE `planet_number` = 3; SELECT * FROM `users` WHERE `company_id` = 99;
IV. Change log
Please see the changelog for more information on what has changed recently.
V. Testing
$ composer test
VI. Contributing
Please see contributing.md for details and a todolist.
VII. Security
If you discover any security related issues, please email dcadavona@gmail.com instead of using the issue tracker.
VIII. Credits
IX. License
MIT. Please see the license file for more information.
doncadavona/eloquenturl 适用场景与选型建议
doncadavona/eloquenturl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「filter」 「laravel」 「query string」 「Eloquenturl」 「query parameters」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 doncadavona/eloquenturl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 doncadavona/eloquenturl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 doncadavona/eloquenturl 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package to retrieve data from Google Search Console
Nova searchable filter for belongsTo relationships.
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
Abstraction Layer to index and search entities
Symfony DataGridBundle
Data provider for yii2
统计信息
- 总下载量: 74
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-20