itsrennyman / laravel-rest-filters

缺少Laravel的RESTful API自动过滤器提供程序。

1.2.1 2020-11-09 17:30 UTC

This package is auto-updated.

Last update: 2024-09-10 03:06:00 UTC


README

Laravel缺少的RESTful API自动过滤器提供程序。

目录

快速入门

安装包。

composer require itsrennyman/laravel-rest-filters

设置过滤

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    return User::withRestFilters()->get();
}

开始过滤

为了过滤,需要在URI查询字符串中传递参数。

// https://:8000/api/users?email=fframi@example.net

[
  {
    "id": 1,
    "name": "Victoria Shanahan",
    "email": "fframi@example.net",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  }
]

完成。如此性感,如此新鲜!

基本过滤器

单字段Where

// https://:8000/api/users?email=fframi@example.net

[
  {
    "id": 1,
    "name": "Victoria Shanahan",
    "email": "fframi@example.net",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  }
]

单字段Where与多个值

// https://:8000/api/users?email=fframi@example.net,hamill.marques@example.com

[
  {
    "id": 1,
    "name": "Victoria Shanahan",
    "email": "fframi@example.net",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  },
  {
    "id": 2,
    "name": "Lenore Stroman II",
    "email": "hamill.marques@example.com",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  }
]

多字段Where

多个字段被连接为多个AND Where。

// https://:8000/api/users?email=aryan@example.net&id=3

[
  {
    "id": 3,
    "name": "Orval Lockman Sr.",
    "email": "aryan@example.net",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  }
]

字段排序

您可以在字段前使用减号运算符,以进行DESC排序。支持多个ORDER BY,使用逗号作为分隔符。

// https://:8000/api/users?sort=-id

[
  {
    "id": 10,
    "name": "Dr. Jason Russel I",
    "email": "qoreilly@example.com",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  },
  {
    "id": 9,
    "name": "Mrs. Yasmine Wintheiser",
    "email": "elmore.krajcik@example.org",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  },
  {
    "id": 8,
    "name": "Garett Botsford Sr.",
    "email": "lueilwitz.kelley@example.com",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  },
  {
    "id": 7,
    "name": "Karli Ondricka",
    "email": "kreiger.andrew@example.com",
    "email_verified_at": "2020-11-02T10:59:10.000000Z",
    "created_at": "2020-11-02T10:59:10.000000Z",
    "updated_at": "2020-11-02T10:59:10.000000Z"
  }
]

字段选择

// https://:8000/api/users?fields=id,name

[
  {
    "id": 1,
    "name": "Mikayla Stanton"
  },
  {
    "id": 2,
    "name": "Mr. Newell Raynor Jr."
  },
  {
    "id": 3,
    "name": "Theodora O'Conner"
  }
]

高级过滤器

大于

// https://:8000/api/users?id=gt:4

[
  {
    "id": 5,
    "name": "Jazmyne Lang",
    "email": "mozelle.bednar@example.org",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 6,
    "name": "Matteo Feest",
    "email": "pschroeder@example.org",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 7,
    "name": "Tabitha Wiegand Jr.",
    "email": "mayer.adrianna@example.org",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  }
]

大于等于

// https://:8000/api/users?id=gte:4

[
  {
    "id": 4,
    "name": "Eleanora Harris",
    "email": "beer.jazmyn@example.net",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 5,
    "name": "Jazmyne Lang",
    "email": "mozelle.bednar@example.org",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 6,
    "name": "Matteo Feest",
    "email": "pschroeder@example.org",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 7,
    "name": "Tabitha Wiegand Jr.",
    "email": "mayer.adrianna@example.org",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  }
]

小于

// https://:8000/api/users?id=lt:4

[
  {
    "id": 1,
    "name": "Miss Damaris Medhurst PhD",
    "email": "ttoy@example.org",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 2,
    "name": "Ms. Sarina Volkman III",
    "email": "twila.fahey@example.net",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 3,
    "name": "Prof. Asha Hane",
    "email": "aliza60@example.com",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  }
]

小于等于

// https://:8000/api/users?id=lte:4

[
  {
    "id": 1,
    "name": "Miss Damaris Medhurst PhD",
    "email": "ttoy@example.org",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 2,
    "name": "Ms. Sarina Volkman III",
    "email": "twila.fahey@example.net",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 3,
    "name": "Prof. Asha Hane",
    "email": "aliza60@example.com",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 4,
    "name": "Eleanora Harris",
    "email": "beer.jazmyn@example.net",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  }
]

LIKE & iLIKE

注意:当前iLIKE在MySQL上不受支持。

使用like属性,查询会搜索传递给参数的全值。如果需要进行搜索,请确保在您的值中添加百分号'%'。

// https://:8000/api/users?name=like:%Mr%

[
  {
    "id": 17,
    "name": "Mrs. Verlie Cummerata",
    "email": "vhessel@example.org",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 19,
    "name": "Mr. Maynard Conn PhD",
    "email": "idooley@example.com",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 26,
    "name": "Mrs. Marcelle Cole IV",
    "email": "gwyman@example.net",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:31.000000Z",
    "updated_at": "2020-11-03T15:43:31.000000Z"
  },
  {
    "id": 68,
    "name": "Mrs. Helga Hansen",
    "email": "meaghan08@example.org",
    "email_verified_at": "2020-11-03T15:43:31.000000Z",
    "created_at": "2020-11-03T15:43:32.000000Z",
    "updated_at": "2020-11-03T15:43:32.000000Z"
  }
]

禁止过滤器和属性

// TODO

贡献

如果您对这个项目有想法或改进,请提交一个Pull Request。