定制 claver/smart-query 二次开发

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

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

claver/smart-query

最新稳定版本:0.0.3

Composer 安装命令:

composer require claver/smart-query

包简介

Making queries much easier in your laravel application

README 文档

README

wakatime

Introduction

Smart-Query is a Laravel package that utilizes spatie-query-builder to enable smart queries with minimal configuration. With this package, you can create powerful, dynamic queries by simply adding the HasSmartQuery trait to your models.

Installation

To install Smart-Query, you need to have a Laravel application set up. Then, you can install the package via composer by running the command:

composer require claver/smart-query

After installing the package, you need to register the SmartQueryServiceProvider in your config > app.php

'providers' => [    
	...,    
	\Claver\SmartQuery\Providers\SmartQueryServiceProvider::class,    
	...
]
php artisan vendor:publish --provider="Spatie\\QueryBuilder\\QueryBuilderServicProvider" --tag="config"

This will publish the query-builder.php file to your config directory.

Usage

To use Smart-Query, add the HasSmartQuery trait to any model you want to enable smart queries on.

use Claver\SmartQuery\HasSmartQuery;

class User extends Model{    
	use HasSmartQuery;
}

This trait provides a resolve method, which you can use to apply smart queries to your model. you can also @override the method name to any name if you do not like the name resolve

use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Claver\SmartQuery\HasSmartQuery;

class User extends Model{    
	use HasSmartQuery;
	
	public function customName(): LengthAwarePaginator|ResourceCollection
    {
        parent::resolve();
    }
}

Finally, you can call your custom method name or resolve if you didn't change the default name.

class UserController extends Controller{    
	public function index(Request $request){        
		$users = (new User())->resolve();        
		return response()->json($users);  
	}
}

The smartQuery method will apply any filters, sorts, includes, and fields specified in the query string to the model.

To learn more about how to make simple and advanced queries, visit Spatie-Query-Builder

You do not have to create your own query builder every single time you need it. Just override these methods.

Method Description
getAllowedFilters Returns an array that includes all the fields that could be filtered
getAllowedIncludes Returns an array that includes all the relationships that could be included
getAllowedSorts Returns an array that includes all the fields that could be sorted. you don't need to include this method because it's dynamically generated

Conclusion

Smart-Query is a powerful package that simplifies creating dynamic queries in Laravel. By using the HasSmartQuery trait, you can take advantage of the spatie-query-builder package and create smart queries with minimal configuration.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固