Laravel makes it incredibly easy to spin up a RESTful CRUD (Create, Read, Update, Delete) API quickly. With route model binding, powerful Eloquent ORM, and simple code organization, you can create a fully-functioning API with just 30 lines of code or less!
In this post, I’ll demonstrate how to create an API for a basic “Article” resource. We’ll make routes to handle CRUD operations, set up an Article model and migration, configure the routes and controllers, and test it out with Postman. Let’s dive in!
First, we need a fresh Laravel install. Make sure Composer is installed globally, then run:
composer create-project laravel/laravel my-api
With our app created, switch directories and launch a dev server:
cd my-api
php artisan serve
Our fresh Laravel install works! Now let’s start building our API.
Our “articles” resource needs a database table to persist. We can quickly spin one up with an Artisan command:
php artisan make:migration create_articles_table --create=articles
This creates the migration file with a table creation stub. Edit the file to make title and body string fields:
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
Now run the migration:
php artisan migrate
Next, we need an Eloquent model to represent articles and interact with the DB table:
php artisan make:model Article
We’re ready for CRUD routes and controllers!
Out of the box, Laravel has route model binding. We can define route params matching Eloquent model names and Laravel resolves them automatically from the DB.
Let’s edit routes/api.php
and define the article resource routes:
// Show all articles
Route::get('/articles', 'ArticleController@index');
// Create a new article
Route::post('/articles', 'ArticleController@store');
// Show, edit and delete a single article
Route::get('/articles/{article}', 'ArticleController@show');
Route::put('/articles/{article}', 'ArticleController@update');
Route::delete('/articles/{article}', 'ArticleController@delete');
That’s the routing done with just 7 lines! Now we need the ArticleController with the basic CRUD methods:
php artisan make:controller ArticleController
Edit app/Http/Controllers/ArticleController.php
:
public function index()
{
return Article::all();
}
public function show(Article $article)
{
return $article;
}
public function store(Request $request)
{
Article::create($request->all());
}
public function update(Request $request, Article $article)
{
$article->update($request->all());
}
public function delete(Article $article)
{
$article->delete();
}
And that’s our basic CRUD API complete with less than 30 lines of code! Let's test it out.
Postman is a popular app for testing APIs.
To test the index route, make a GET
request to /api/articles
. It should return an empty array for now.
Make a POST
request to the same URL with a title and body payload to create an article. Confirm it was inserted by checking the database directly.
Make more requests like GET by id, PUT to update an article, DELETE to remove one etc. Verify after each operation that data is changing as expected in DB.
With Postman, we can confirm our Laravel CRUD API works perfectly!
And that’s it! To quickly recap what we did:
All in just 30 lines of code thanks to Laravel's simplicity and elegance!
Some next steps with this API could be:
And that’s our basic CRUD API complete with less than 30 lines of code thanks to Laravel's simplicity and elegance! As you can see, it’s simple to spin up a powerful API quickly in Laravel.
However, building a robust, production-ready API involves much more work. If you don't have the time or skills to handle all aspects of API development, you may want to hand off the heavy lifting to expert Laravel developers.
Give it a try and see how much faster you can launch your API project! Let me know if you have any other questions.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community