定制 wpzag/laravel-query-builder 二次开发

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

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

wpzag/laravel-query-builder

Composer 安装命令:

composer require wpzag/laravel-query-builder

包简介

Build up Eloquent queries from the Query string parameters of the request .

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package allows you to filter, sort, append, paginate and load relations based on the request query parameters.

Installation

You can install the package via composer:

composer require wpzag/laravel-query-builder

You can publish the config file with:

php artisan vendor:publish --tag="query-builder-config"

Usage

Add config for your model in query-builder.php

'models' => [
    User::class => [
       'includes' => ['posts.comments.author', ],
       'appends' => ['fullname','avatar'],
       'filterable' => ['*:except:role', 'posts.title', 'posts.body', 'posts.created_at'],
       'sortable' => ['*'],
       'max_per_page' => 20             
     ],   
 ]       

[ * ] means all the model's attribute except hidden .

[ *:except:column1,column2 ] You can also exclude some attributes by adding :except: then comma separated attributes .

[ age:exact ] If you prefixed the attribute with :extact it will use the ( = ) operator instead of ( LIKE ) operator in the query.

Usage

$builder = QueryBuilder::for(User:class); //This applies includes/filters/sorts to the query

$builder->query() // Returns the query 

$builder->get() // Returns the eloquent collection

$builder->withPagination() // Applies pagination to the query

$builder->withAppends() // Applies appends to the query

$builder->withPaginationAndAppends() // Applies pagination & appends to the query

You can also add custom query to query-builder

QueryBuilder::for(User:class)->query()->where('id', '>', 1)->get();


// Or if you are using  withPagination(),withAppends() or withPaginationAndAppends() :
QueryBuilder::for(User:class, function($query){

	//extra queries here
	
	return $next($query);
})->withPaginationAndAppends();

Examples:

Filtering

users?filter[name]=john,rose

// User::where('name','like','%john%')
//       ->orWhere('name','like','%rose%')->get();
users?filter[name,username]=john

// User::where('name','like','%john%')
//       ->orWhere('username','like','%rose%')->get();
users?filter[name]=john,rose&filter[age]=gt.20

// User::where('name','like','%john%')
//       ->orWhere('name','like','%rose%')
//       ->where('age','>',20)->get();
users?filter[age][between]=[29,40]

// User::whereBetween('age', [29, 40])->get();
users?filter[email_verified_at][empty]

// User::whereNull('email_verified_at')->get();
users?filter[created_at][date]=2020-01-01

// User::whereDate('created_at', '2020-01-01')->get();
users?filter[posts.created_at]=lt.2020-01-01

// User::whereHas('posts', function($query) {
//     $query->whereDate('created_at', '<', '2020-01-01');
// })->get();
users?filter[role][not-in]=[admin,teacher,student]

// User::whereNotIn('role', ['admin', 'teacher', 'student'])->get();

Sorting

users?sort=-created_at

// User::orderBy('created_at', 'desc')->get();
users?sort=-statues,name

// User::orderBy('statues', 'desc')->orderBy('name', 'asc')->get();

Appending

users?append=fullname

// User::get()->append('fullname')

Loading relations

users?include=profile,posts.comments.author

// User::with('profile','posts.comments.author')->get();

Pagination

users?page=2&limit=10

// User::paginate(10);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-07-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固