当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

laravel-query-builder

Easily build Eloquent queries from API requests
授权协议 MIT License
开发语言 PHP
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 吴俊晤
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Build Eloquent queries from API requests

This package allows you to filter, sort and include eloquent relations based on a request. The QueryBuilder used in this package extends Laravel's default Eloquent builder. This means all your favorite methods and macros are still available. Query parameter names follow the JSON API specification as closely as possible.

The master branch is a WIP for v4. For the latested tagged version of the package, please switch to the v3 branch.

Basic usage

Filter a query based on a request: /users?filter[name]=John:

use Spatie\QueryBuilder\QueryBuilder;

$users = QueryBuilder::for(User::class)
    ->allowedFilters('name')
    ->get();

// all `User`s that contain the string "John" in their name

Read more about filtering features like: partial filters, exact filters, scope filters, custom filters, ignored values, default filter values, ...

Including relations based on a request: /users?include=posts:

$users = QueryBuilder::for(User::class)
    ->allowedIncludes('posts')
    ->get();

// all `User`s with their `posts` loaded

Read more about include features like: including nested relationships, including relationship count, custom includes, ...

Sorting a query based on a request: /users?sort=id:

$users = QueryBuilder::for(User::class)
    ->allowedSorts('id')
    ->get();

// all `User`s sorted by ascending id

Read more about sorting features like: custom sorts, sort direction, ...

Works together nicely with existing queries:

$query = User::where('active', true);

$userQuery = QueryBuilder::for($query) // start from an existing Builder instance
    ->withTrashed() // use your existing scopes
    ->allowedIncludes('posts', 'permissions')
    ->where('score', '>', 42); // chain on any of Laravel's query builder methods

Selecting fields for a query: /users?fields[users]=id,email

$users = QueryBuilder::for(User::class)
    ->allowedFields(['id', 'email'])
    ->get();

// the fetched `User`s will only have their id & email set

Read more about selecting fields.

Appending attributes to a query: /users?append=full_name

$users = QueryBuilder::for(User::class)
    ->allowedAppends('full_name')
    ->get()
    ->toJson();

// the resulting JSON will have the `getFullNameAttribute` attributes included

Read more about appending attributes.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-query-builder

Read the installation notes on the docs site: https://docs.spatie.be/laravel-query-builder/v3/installation-setup.

Documentation

You can find the documentation on https://docs.spatie.be/laravel-query-builder/v3.

Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving the media library? Feel free to create an issue on GitHub, we'll try to address it as soon as possible.

If you've found a bug regarding security please mail freek@spatie.be instead of using the issue tracker.

Upgrading

Please see UPGRADING.md for details.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Credits

License

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

  • 读 DB::table('users')->get(); // 返回包含对象的集合 DB::table('users')->distinct()->get(); DB::table('users')->select('id', 'age as user_age')->get(); DB::table('users')->where('name

  • 上次干这事已经是一年前了,之前的做法特别的繁琐、冗余,具体就是创建一个自定义 Builder 类,继承自 Query\Builder,然后覆盖 Connection 里面获取 Builder 的方法,返回自定义的 Builder,还有其他一系列很长的步骤。   下面是之前的做法: (算了,还是不说了,太蠢),总之,多看看源码有好处   就说最优雅的解决方法吧: laravel 中提供了 Macro

  • 概述: 项目当中经常用到复杂SQL带有子查询,而Laravel的查询构造器并没有直接提供转化成子查询的方法,或者说尽管提供了方法,可是给出来的例子却不太具体,对刚上手的新手来说非常不友好,本文章主要是聊一聊Laravel如何构造各种带有子查询的SQL。 准备: 首先,Laravel一般实现子查询的两种方式: 1、toSql()+getQuery()+raw()方法: toSql()方法的作用是为了

  • 简介 Builder 就是查询到 SQL 转换的纽带! 这章很难,劝你放弃阅读 ?‍♀️?‍♂️ 老板和打工仔的故事 `Eloquent Builder` 是我们在使用 `Laravel` 模型进行查询的时候调用的对象,转换 `SQL` 最终是调用了 `Query Builder` 对象的服务。 所以我们将介绍两个 `Builder` 对象。 复制代码 Query Builder 指 I

  • Basics 最简单的导出方法是创建一个自定义的导出类, 这里我们使用发票导出作为示例. 在 App/Exports 下创建一个 InvoicesExport 类 namespace App\Exports; use Maatwebsite\Excel\Concerns\FromCollection; class InvoicesExport implements FromCollection

 相关资料
  • Laravel Eloquent Query Cache Laravel Eloquent Query Cache brings back the remember() functionality that has been removed from Laravel a long time ago.It adds caching functionalities directly on the El

  • 问题内容: 我有一个现有的产品变型方案。 我想创建每个生产时间,数量和变化选项的组合。 我将通过访问产品的数量,生产时间,差异和差异选项来创建选择表单。 table_groups table_days table_quantities table_attributes table_attribute_values 我准备了一个示例架构。但是,我没有得到想要的结果。 SQL小提琴 我做了很多事情:

  • Match All Query 最简单的查询,它匹配所有文档 查看 Match All Query QueryBuilder qb = matchAllQuery();

  • Query是一个抽象类,包含各种实用程序方法,是Lucene在搜索过程中使用的所有类型查询的父级。 Class 声明 (Class Declaration) 以下是org.apache.lucene.search.Query类的声明 - public abstract class Query extends Object implements Serializable, Clon

  • Every GraphQL schema has a root type for both queries and mutations. The query type defines GraphQL operations that retrieve data from the server. Fieldsaction (Action) 动作查询接口 Argument Type Descriptio

  • 本文向大家介绍coldfusion Query,包括了coldfusion Query的使用技巧和注意事项,需要的朋友参考一下 示例 考虑表dbo.state_zip,其中包含列city,statecode并且zipcode,有超过80,000条记录。 参数 属性 需要 类型 默认 描述 询问 真正 串 查询对象的变量名。 起始行 假 数字 查询对象的起始行索引。 row 假 数字 查询对象的结束