The Missing RESTFul API AutoFilters Provider for Laravel.
composer require itsrennyman/laravel-rest-filters
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return User::withRestFilters()->get();
}
In order to Filter, pass the parameters in the URI Querystring.
// http://localhost:8000/api/[email protected]
[
{
"id": 1,
"name": "Victoria Shanahan",
"email": "[email protected]",
"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"
}
]
And Done. So Sexy So Fresh!
// http://localhost:8000/api/[email protected]
[
{
"id": 1,
"name": "Victoria Shanahan",
"email": "[email protected]",
"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"
}
]
// http://localhost:8000/api/[email protected],[email protected]
[
{
"id": 1,
"name": "Victoria Shanahan",
"email": "[email protected]",
"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": "[email protected]",
"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"
}
]
Multiple fields are joined as multiple AND wheres.
// http://localhost:8000/api/[email protected]&id=3
[
{
"id": 3,
"name": "Orval Lockman Sr.",
"email": "[email protected]",
"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"
}
]
You can use the minus operator in front of the field, in order to do the DESC sorting. Multiple order by are supported, use the comma as separator.
// http://localhost:8000/api/users?sort=-id
[
{
"id": 10,
"name": "Dr. Jason Russel I",
"email": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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"
}
]
// http://localhost:8000/api/users?fields=id,name
[
{
"id": 1,
"name": "Mikayla Stanton"
},
{
"id": 2,
"name": "Mr. Newell Raynor Jr."
},
{
"id": 3,
"name": "Theodora O'Conner"
}
]
// http://localhost:8000/api/users?id=gt:4
[
{
"id": 5,
"name": "Jazmyne Lang",
"email": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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"
}
]
// http://localhost:8000/api/users?id=gte:4
[
{
"id": 4,
"name": "Eleanora Harris",
"email": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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"
}
]
// http://localhost:8000/api/users?id=lt:4
[
{
"id": 1,
"name": "Miss Damaris Medhurst PhD",
"email": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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"
}
]
// http://localhost:8000/api/users?id=lte:4
[
{
"id": 1,
"name": "Miss Damaris Medhurst PhD",
"email": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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"
}
]
NB: Currently iLike is not supported on MySQL.
With the like attribute, the query search the full value passed as parameter. If you need to do search, be sure to add the percentage '%' to your values.
// http://localhost:8000/api/users?name=like:%Mr%
[
{
"id": 17,
"name": "Mrs. Verlie Cummerata",
"email": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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
If you have ideas, or improvements for this Project, please open a Pull Request.