DEV Community

Cover image for Creating A Basic REST API in Laravel 9
Emmanuel Larbi
Emmanuel Larbi

Posted on

Creating A Basic REST API in Laravel 9

In this tutorial, I am going to take you through the basics in building a REST API in Laravel. But before that lets take a quick glance into what a REST API is.

What is REST API?

A REST API, commonly referred to as a RESTful API, is a web API that complies with the restrictions of the REST architectural style and enables communication with RESTful web services. Computer scientist Roy Fielding came up with the acronym REST, which stands for representational state transfer.

More on REST APIs

Let get started.

Create a new Laravel project

composer create-project laravel/laravel basic_laravel_api
Enter fullscreen mode Exit fullscreen mode

or using Laravel Installer

laravel new basic_laravel_api
Enter fullscreen mode Exit fullscreen mode

and move into the directory

cd basic_laravel_api
Enter fullscreen mode Exit fullscreen mode

Create a model and migration

php artisan make:model Post -m
Enter fullscreen mode Exit fullscreen mode

Update and migrate your database

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=basic_laravel_api
DB_USERNAME=root
DB_PASSWORD=
Enter fullscreen mode Exit fullscreen mode

Post table update

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
};

Enter fullscreen mode Exit fullscreen mode

Then run your migrations

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Create and Update your Controller

php artisan make:controller Api\\PostController --model=Post
Enter fullscreen mode Exit fullscreen mode

Api Controller

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $posts = Post::all();

        return response()->json([
            'status' => true,
            'post' => $posts
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $post = Post::create(request()->all());

        return response()->json([
            'stauts' => true,
            'post' => $post
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function show(Post $post)
    {
        $post = Post::find($post)->first();

        return response()->json([
            'status' => true,
            'post' => $post
        ]);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Post $post)
    {
        $post->update(request()->all());
        return response()->json([
            'status' => true,
            'post' => $post
        ]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function destroy(Post $post)
    {
        $post->delete();

        return response()->json([
            'status' => true,
        ]);
    }
}

Enter fullscreen mode Exit fullscreen mode

Define your routes

open routes/api.php

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\PostController;

Route::apiResource('v1/posts', PostController::class);

Enter fullscreen mode Exit fullscreen mode

Start Laravel Server

php artisan serve
Enter fullscreen mode Exit fullscreen mode

Preview using Postman

GET all Post

GET all Post

Create a Post. POST

Create(POST) a Post

GET a specific post

GET a single post

Update a post. PUT/PATCH

Update a post

DELETE a post

Delete a post

Top comments (0)