Interested in generating passive income? Join our partnership program and receive a commission on each new client referral. Learn more.
9 min read
Interested in generating passive income? Join our partnership program and receive a commission on each new client referral. Learn more.
API Platform might seem, at first glance, like just another tool to speed up the process of building CRUD endpoints. But after diving into the documentation and experimenting with its features, it becomes clear that it’s much more than that. This package offers a fresh perspective on how Laravel applications can be built with far less effort.
At its core, API Platform is all about making API development easier in Laravel. It automatically registers routes and creates controllers based on models, generates API documentation effortlessly, offers GraphQL support with no extra effort, and even produces simple frontend code for frameworks like Nuxt, Next, and others. For example, it’s possible to build a basic CRM in just 30 minutes - without writing a single controller or registering a route manually.
Let’s go through the core functionalities of the Laravel API platform, highlight essential features, and outline some of its limitations. By the end, you'll have a clear understanding of what API Platform is, how it simplifies API development, and when it’s the right choice for a project.
To better understand how API Platform works, we’ll create a simple blog as an example. Our blog will consist of a basic setup with a Post model and a Category model.
This structure is enough to get started but we’ll add additional components on the go to demonstrate all the juicy parts of this package.
Here’s how the migration for the Post model looks:
Schema::create('post', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug');
$table->string('description');
$table->datetime('published_at');
$table->foreignIdFor(User::class);
$table->foreignIdFor(Category::class);
$table->timestamps();
});
And here’s the migration for the Category model:
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->timestamps();
});
Next, we’ll define relationships between our models. In the Category model, we’ll add:
public function posts()
{
return $this->hasMany(Post::class);
}
And in the Post model:
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
With this, we have a simple and functional structure.
Before diving into the package’s functionalities, you’ll need to install it:
composer require api-platform/laravel
After installation, publish assets and config:
php artisan api-platform:install
Now, everything is set up, and we’re ready to explore what this package can do.
First, let’s take a look at the beginning of this package: generating endpoints just from Eloquent models.
Imagine you’re building an API to connect with a Vue.js application. Typically, you’d need to manually register routes and create controllers. With API Platform, it’s as simple as adding the #[ApiResource] attribute to your model:
#[ApiResource]
class Post extends Model
{
…
}
That’s it! Once this attribute is added, running php artisan route:list will display a set of ready-to-use routes automatically generated for your model.
These routes are fully functional, so you can start testing them right away using Postman or any other API tool.
Having a well-organized documentation page is essential for any API application. It provides a clear overview of all available routes and their parameters. This package takes care of that for you by automatically generating documentation using Swagger UI. Now, if we access localhost/api, we will see this:
With just one attribute, we have controllers & routes registered and beautiful API docs that allow you to test API endpoints on the same page.
If you need to use Graphql, you should start by installing this package:
composer require api-platform/graphql:^4
Next, open config/api-platform.php and simply set:
'enabled' => true,
Once that’s done, you’ll have full GraphQL support. It’s that simple.
Validating input is a crucial part of any API, and API Platform makes it easy to register request validations. It follows Laravel’s standard naming conventions, so once you create a validation request, it’s automatically detected.
In our case, however, the request wasn’t picked up automatically, so we added the validation rules directly to the attribute:
#[ApiResource(
rules: PostFormRequest::class
)]
For this, we created the PostFormRequest.php file with the following rules.
'title' => 'required|string|max:255',
'slug' => 'required|string|max:255|unique:posts,slug',
'description' => 'nullable|string',
'published_at' => 'nullable|date',
'user_id' => 'required|exists:users,id',
'category_id' => 'required|exists:categories,id',
With this setup, the validation rules will be automatically applied whenever you call the relevant methods.
We’ve just scratched the surface with one parameter here: rules. But API Platform offers a variety of other parameters to further customize your API:
Middleware – Add middleware directly via attributes
Description – Provide a description for each route to display in the documentation
Per Request Configuration – Exclude unwanted routes (e.g., exclude a PATCH endpoint)
Authorization – Add authorization properties, such as gates
Pagination Properties – Define default pagination settings (e.g., how many items to retrieve per request)
There are more options, but we won’t cover all of them here. Be sure to check out the documentation for additional examples and detailed descriptions before getting started with the package.
In the PHP community, it’s not uncommon to hear developers express dislike for attributes. If you’re one of them, don’t worry, there is another way of defining the same functionality within models:
public static function apiResource(): ApiResource
{
return new ApiResource([
//Same configuration
]);
}
With this approach, you achieve the same functionality, but instead of using attributes, you define a simple function.
It’s hard to believe all the functionality this package offers, but yes, you can actually auto-generate both the admin panel and the client-side application based on your API routes.
Admin side:
Client side:
With just a few simple commands, you’ll have a fully functional admin panel and client-side platform for both desktop and mobile. It supports frameworks like React Native, Next.js, Nuxt, Quasar, and more. Plus, real-time updates are built-in, so when you modify data, changes will automatically reflect in your React Native app without needing a refresh.
The Laravel API Platform offers some truly impressive functionalities that allow us to create content in extremely short periods of time.
However, it’s important to address a few limitations. The package currently doesn’t support resources, which is something many Laravel developers might miss. Resources are a crucial part of Laravel applications, especially when working with APIs.
Another limitation involves related resources being automatically loaded with the results. For example, a simple categories GET endpoint might return a list of posts by default:
updatedAt": "2024-12-25T08:16:36+00:00",
"posts": [
"/api/posts/10",
"/api/posts/17",
"/api/posts/31",
"/api/posts/34",
"/api/posts/50",
"/api/posts/53",
"/api/posts/59",
"/api/posts/65",
"/api/posts/70",
"/api/posts/72",
"/api/posts/76",
"/api/posts/84",
"/api/posts/88",
"/api/posts/92"
]
While this can be useful, it lacks flexibility. Developers can control relationships, but the inability to show/hide them or modify how they are presented makes it feel somewhat rigid. Introducing resource support would provide the flexibility needed to better control the output.
Overall, we’re happy to see the Laravel API platform as a crucial addition to the Laravel ecosystem. However, it’s still a work in progress and may not be suitable for large-scale Laravel SaaS applications where heavy customization is required. Development is in the active phase, and hopefully, full compatibility with all Laravel features will be available soon.
We are a 200+ people agency and provide product design, software development, and creative growth marketing services to companies ranging from fresh startups to established enterprises. Our work has earned us 100+ international awards, partnerships with Laravel, Vue, Meta, and Google, and the title of Georgia’s agency of the year in 2019 and 2021.