Creating a RESTful API with Laravel
Tags: restful, laravel, api, beginners
Published: 2023-03-06 20:41:49

## 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:

- [Composer](https://getcomposer.org/)
- [Laravel](https://laravel.com/docs/10.x/installation)

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

## Steps

1). Create a new Laravel project

```bash
laravel new my-api
```
or 

```bash
composer create-project laravel/laravel my-api
```
2). Create a new controller for our API endpoints

```bash
php artisan make:controller ApiController
```
3). Define our API routes in _**routes/api.php**_

```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']);

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

```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();
    }
}

```
5). Database configuration in .env file

```env
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
```

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

```bash
php artisan migrate
```
7). Test our API using a tool like [Postman](https://www.postman.com/)
Before testing, you'll need to serve the project.

```bash
php artisan serve
```
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](https://github.com/J-Sandaruwan/restful_api)

Thank you,
J-Sandaruwan.
[linkedin](https://www.linkedin.com/in/j-sandaruwan/)