aminsamadzadeh/simorgh
Composer 安装命令:
composer require aminsamadzadeh/simorgh
包简介
add auto seraching from request
README 文档
README
This package allows you to create simple search query just with query string.
Installing
just run below command:
composer require aminsamadzadeh/simorgh
Usage
You can use this package in your controller when you want to search somethig. In first time you must add this package in model that you want to serching.
<?php namespace App; use Illuminate\Database\Eloquent\Model; use AminSamadzadeh\Simorgh\Filterable; class User extends Model { use Filterable; }
If you want just spesific filed just can be search you must add $filterable in your model.
<?php namespace App; use Illuminate\Database\Eloquent\Model; use AminSamadzadeh\Simorgh\Filterable; class User extends Model { use Filterable; protected $filterable = ['name', 'email']; }
In controller you can use this functionality.
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use App\User; class UserController extends Controller { public function index() { $users = User::filter(request()->all())->get(); return view('users.index', compact('users')); } }
Queries
Simple Where
Request query string with simorgh:
/users?filter[name]=amin
Eloquent Query without simorgh:
$users = User::where('name', 'amin')->get();
SQL Raw Query:
select * from "users" where "name" = 'amin'
Relational
Request query string with simorgh:
/articles?filter[images.id]=1
Eloquent Query without simorgh:
$articles = Article::whereHas('images',function ($q) { $q->where($id, 1); } );
SQL Raw Query:
select * from "articles" where exists (select * from "images" where "articles"."id" = "images"."article_id" and "id" = 1)
Range (Between)
Request query string with simorgh:
/users?filter[created_at]=(1970-01-01,1970-02-01)
Eloquent Query without simorgh:
$users = User::whereBetween('created_at', ['1970-01-01', '1970-02-01'])->get();
SQL Raw Query:
select * from "users" where "created_at" between 1970-01-01 and 1970-02-01
Array
Request query string with simorgh:
/users?filter[id][]=1&filter[id][]=2&filter[id][]=3
Eloquent Query without simorgh:
$users = User::where('id', [1,2,3])->get();
SQL Raw Query:
select * from "users" where "id" in (1, 2, 3)
Sort
Request query string with simorgh:
/users?filter[sort]=created_at&filter-meta[sort-order]=desc
Eloquent Query without simorgh:
$users = User::orderBy('created_at', 'desc')->get();
SQL Raw Query:
select * from "users" order by "created_at" desc
Settings
Meta
To change oporator of query you can use meta in query string.
https://host.com/users?filter[name]=amin?filter-meta[name][op]=like
set default meta in model:
<?php namespace App; use Illuminate\Database\Eloquent\Model; use AminSamadzadeh\Simorgh\Filterable; class User extends Model { use Filterable; protected $filterable = ['name', 'email']; protected $filterMeta = ['name' => ['op' => 'like']]; }
Note: Support oporators like, =
Alias for query string names(filter and filter meta)
If want change filter and filter meta query string name you can add changed name to model.
. . . class User extends Model { use Filterable; protected $filter_name = 'f'; protected $filter_meta = 'fm'; }
aminsamadzadeh/simorgh 适用场景与选型建议
aminsamadzadeh/simorgh 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 91 次下载、GitHub Stars 达 11, 最近一次更新时间为 2020 年 07 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 aminsamadzadeh/simorgh 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aminsamadzadeh/simorgh 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 91
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-10