Build Simple REST API with Laravel

Photo by Artur Shamsutdinov on Unsplash

REST (Representational State Transfer) is an architectural method of communication that uses HTTP protocol for data exchange. Application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. In short, API can make other applications, whether it’s a mobile apps or something else, can communicate with our application.

In this post I want to share how to create a REST API in Laravel with very simple way.

First create a new project or you can skip this if you already have one.

composer create-project laravel/laravel laravel-api --prefer-dist

Set up your database connection in the .env and then create migration for some table, in this I will use posts table as an example.

php artisan make:migration create_posts_table

In the posts migration file :

<?phpuse Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug');
$table->string('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}

Run migrate with php artisan migrate command.

Create some models and controllers

php artisan model Post -c

Write all, show, store, update and destroy function in the controller, in the PostController will be something like this :

Add guarded or fillable property into Post model, because in the controller we use mass assignment.

protected $guarded = ['id'];

Modify the routes in api.php

use App\Http\Controllers\PostController;Route::group(['prefix'=>'post'], function () {
Route::get('/', [PostController::class, 'index']);
Route::get('/{id}', [PostController::class, 'show']);
Route::post('/', [PostController::class, 'store']);
Route::put('/{id}', [PostController::class, 'update']);
Route::delete('/{id}', [PostController::class, 'destroy']);
});

Test
We need application like postman or insomnia or something else for testing purpose. You can download postman from this link.

Get all posts data
Get specific post data
Store a new post data
Update specific post
Delete specific post data

Thats all that I can share in this post. Maybe in the next post I will share about API authentication in Laravel using passport.

Thanks for reading my post, please feel free to leave a comment if you have any suggestions or questions. See ya…

References
1. https://en.wikipedia.org/wiki/API

--

--

--

Backend Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building Apps for Multiple Platforms: Which Frameworks to Use

From Concept to Completion: The Journey of an App- Part II

Deploy Spring Boot application and MongoDB as Containers Using Docker

Solvathon — Let’s Solve by Hackathon

How to run Rails application with URL prefix

Why Did Kotlin Succeed — And Scala Not

READ/DOWNLOAD%? Automata, Computability and Comple

DevOps: All You Need to Know

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Awang Trisakti

Awang Trisakti

Backend Engineer

More from Medium

Build REST API with Laravel

Google Sheet Integration in Laravel

Laravel email verification during registration via secret key

Develop clean MVC web applications with Laravel 9