Generate Apis with ease with Laravel Rest Api

Packages

September 8th, 2023

Generate Apis with ease with Laravel Rest Api

Laravel Rest Api is a package designed to help you generate your Api in a powerful way without restructuring your codebase. It integrates the following features:

  • Full REST API
  • Automatic gates integration
  • All Laravel's relationships support
  • Fully customisable
  • Secured through the whole process
  • Complex filtering operations
  • Mutate multiple models using one endpoint
  • Automatic documentation generation with customisation support

Installing Laravel Rest Api

Install the package using composer:

composer require lomkit/laravel-rest-api

To start using the package, you have a quick start command setting you up a full support API around the User model:

php artisan rest:quick-start

This command will generate the UserResource and UsersController classes and register them in your api.php route file.

And you're done! The endpoints are fully registered; have a look at them using the php artisan route:list command:

+---------+--------------------------------+----------------------+
| Method | URI | Name |
+---------+--------------------------------+----------------------+
| GET | api/users | api.users.detail |
| POST | api/users/search | api.users.search |
| POST | api/users/actions/{action} | api.users.operate |
| POST | api/users/mutate | api.users.mutate |
| DELETE | api/users | api.users.destroy |
+---------+--------------------------------+----------------------+

To have a look at what's possible with these freshly registered endpoints, please have a look at the endpoint part of the documentation.

Here's an example of the search endpoint possibilities:

// (POST) api/posts/search
{
"scopes": [
{"name": "withTrashed", "parameters": [true]}
],
"filters": [
{
"field": "id", "operator": ">", "value": 1, "type": "or"
},
{
"nested": [
{"field": "user.id", "operator": "<", "value": 2},
{"field": "id", "operator": ">", "value": 100, "type": "or"}
]
}
],
"sorts": [
{"field": "user_id", "direction": "desc"},
{"field": "id", "direction": "asc"}
],
"selects": [
{"field": "id"}
],
"includes": [
{
"relation": "posts",
"filters": [
{"field": "id", "operator": "in", "value": [1, 3]}
],
"limit": 2
},
{
"relation": "user",
"filters": [
{
"field": "languages.pivot.boolean",
"operator": "=",
"value": true
}
]
}
],
"aggregates": [
{
"relation": "stars",
"type": "max",
"field": "rate",
"filters": [
{"field": "approved", "value": true}
]
}
],
"instructions": [
{
"name": "odd-even-id",
"fields": [
{ "name": "type", "value": "odd" }
]
}
],
"page": 2,
"limit": 10
}

Links

The Laravel Rest Api package and documentation can be found on GitHub at lomkit/laravel-rest-api.

What's next

  • Frontend metrics support
  • Plain text search using Laravel Scout
  • Aliases on includes / aggregates

Filed in:

Gautier Deleglise

Passionate Laravel dev dedicated to helping enhancing the Laravel ecosystem for a seamless developer experience. 🚀