sympla / the-brick
提供内容协商的库
1.0.11
2018-08-31 20:55 UTC
Requires (Dev)
- phpunit/phpunit: ~6.0
This package is not auto-updated.
Last update: 2024-09-29 04:43:04 UTC
README
我们都希望能有那位聪明的邻家酒吧老板,在几轮酒之后给我们提供建议。
这个库可以帮助协商与优雅模型(字段、关系和过滤器)相关的内容
安装
使用composer安装此包
$ composer require sympla/the-brick ~1.0
发布包配置:$ php artisan vendor:publish --provider="Sympla\Search\Search\SearchServiceProvider"
这就完成了。
简单用法
public function index(Request $request) { $res = $res->negotiate('Models\User'); return response()->json($res); }
扩展模型上的协商
namespace App\Models; use Sympla\Search\Model\Model; class User extends Model { }
创建您的过滤器
public function scopeFilterByPhone($query) { return $query->where('phone', '<>', ''); }
现在您只需用您的过滤器和请求中希望返回的字段调用您的路由即可
https://:8000/api/users?&fields=name,email&filters=filterByPhone
参数列表
fields (字符串)
搜索的字段和关系列表:id,name,relationName(id,email)
filters (字符串)
将在您的模型中调用的作用域列表:filterByName
public function scopeFilterByName($query) { return $query->where('name', Request::get('name')); }
orderBy (字符串)
查询排序字段
sort (字符串)
升序或降序排序
limit (整数)
查询记录限制
noPaginate (布尔值)
指示查询是否分页
size (整数)
指示每页的记录数
debug (布尔值)
返回查询生成的所有sql的数组
与Laravel一起使用
服务提供者(在Laravel 5.5中可选)
Composer安装或更新您的包后,您需要将别名或注册您的包添加到Laravel中。打开config/app.php,找到aliases键并添加
Sympla\Search\Search\SearchServiceProvider::class,
生成文档
Docblock变量
- @negotiate : 通知使用了哪个模型
- @negotiateDesc : 方法/过滤器的描述
如何使用
将文档变量添加到您的docblock中
控制器
/** * @negotiate Models\User * @negotiateDesc Get and filter all users */ public function index(Request $request) { $res = $res->negotiate('Models\User'); return response()->json($res); }
模型
/** * @negotiateDesc Get users with phones */ public function scopeFilterByPhone($query) { return $query->where('phone', '<>', ''); }
生成文档
执行此命令
php artisan negotiate:docgen
访问文档
通过URL https://:8000/_negotiate/documentation
访问文档
联系方式
Bruno Coelho bruno.coelho@sympla.com.br
Marcus Campos marcus.campos@sympla.com.br
许可证
该项目根据MIT许可证分发。有关更多信息,请参阅[LICENSE][LICENSE.md]。