定制 aminsamadzadeh/simorgh 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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';

}

统计信息

  • 总下载量: 91
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-07-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固