Creating a RESTful API with Laravel

jsandaruwan

J-Sandaruwan

Posted on March 6, 2023

Creating a RESTful API with Laravel

Introduction

In this tutorial, we will be creating a RESTful API using Laravel, a popular PHP web framework. A RESTful API allows for easy communication between client-side applications and servers. Laravel provides a robust set of tools to create and manage APIs quickly and easily.

Prerequisites

Before we begin, you should have the following installed:

You should also have a basic understanding of PHP and web development.

Steps

1). Create a new Laravel project

laravel new my-api
Enter fullscreen mode Exit fullscreen mode

or

composer create-project laravel/laravel my-api
Enter fullscreen mode Exit fullscreen mode

2). Create a new controller for our API endpoints

php artisan make:controller ApiController
Enter fullscreen mode Exit fullscreen mode

3). Define our API routes in routes/api.php

use App\Http\Controllers\ApiController;

Route::get('/users', [ApiController::class, 'getUsers']);
Route::get('/users/{id}', [ApiController::class, 'getUserById']);
Route::post('/users', [ApiController::class, 'createUser']);
Route::put('/users/{id}', [ApiController::class, 'updateUser']);
Route::delete('/users/{id}', [ApiController::class, 'deleteUser']);

Enter fullscreen mode Exit fullscreen mode

4). Define our API methods in app/Http/Controllers/ApiController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class ApiController extends Controller
{
    public function getUsers()
    {
        return User::all();
    }

    public function getUserById($id)
    {
        return User::find($id);
    }

    public function createUser(Request $request)
    {
        $user = new User;
        $user->name = $request->input('name');
        $user->email = $request->input('email');
        $user->password = $request->input('password');
        $user->save();

        return $user;
    }

    public function updateUser(Request $request, $id)
    {
        $user = User::find($id);
        $user->name = $request->input('name');
        $user->email = $request->input('email');
        $user->password = $request->input('password');
        $user->save();

        return $user;
    }

    public function deleteUser($id)
    {
        $user = User::find($id);
        $user->delete();

        return response()->noContent();
    }
}

Enter fullscreen mode Exit fullscreen mode

5). Database configuration in .env file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_user_name
DB_PASSWORD=your_database_password
Enter fullscreen mode Exit fullscreen mode

6). To create the database, please open the terminal inside the project and run this command.

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

7). Test our API using a tool like Postman
Before testing, you'll need to serve the project.

php artisan serve
Enter fullscreen mode Exit fullscreen mode

Once that's done, you can obtain the project server URL. Append "/api/" to the project link and use the resulting link to run the test.

  • GET /users to retrieve all users
  • GET /users/{id} to retrieve a specific user
  • POST /users to create a new user
  • PUT /users/{id} to update an existing user
  • DELETE /users/{id} to delete a user

Conclusion

In this tutorial, we learned how to create a RESTful API with Laravel. We covered creating a new Laravel project, defining API routes and methods, and testing our API using a tool like Postman. Laravel provides a powerful and intuitive framework for creating and managing APIs, making it an excellent choice for web development projects.

Example project:-
restful_api By J-Sandaruwan

Thank you,
J-Sandaruwan.
linkedin

💖 💪 🙅 🚩
jsandaruwan
J-Sandaruwan

Posted on March 6, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related