Create An API Using Laravel
Tags: webdev, programming, api, laravel
Published: 2023-01-10 16:41:01



## What is Laravel?

**Laravel** is a PHP framework developed by **Taylor Otwell**. It is a set of PHP files that will facilitate programming, thanks to its elegant and expressive syntax. 
It provides MVC implementation that helps you organizing your code for easier maintenance.
You will be able to create modern web application, including such things such as APIs or web services.

## RESTful APIs : 
There are many types of APIs but we will focus on RESTful APIs.
Well, API **(Application Programming Interface)**  is a set of protocols and tools that allows applications to communicate and exchanges multiple data and services. While REST stands for **Representational State Transfer**.
The picture bellow may explain more about RESTful API and how it works : 

👇🏻👇🏻👇🏻

![RESTful Api](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bl8hxn5cxys4v3al1edo.png)



## STEP1 : Install Laravel 

To install the latest version of Laravel, you need to run the command bellow : 

`composer create-project --prefer-dist laravel/laravel laravel_api_example`

Once your application has been created, you could start the local development server using the Laravel artisan CLI serve : 

```
cd laravel_api_example

php artisan serve
```
 
Indeed, you have to set up your database informations in the **.env** file 


![database](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwrlynquneppi5e5a0lq.PNG)

## STEP2 : Plan your API 

Try to make your base URL clear and simple.
In our tutorial we will design API for **Events**.
📌 **NOTE!!!! => Keep verbs out of your base URLs.**

Thus, if we would like for example to create an API to get your list of Events, the URL should look like that : 

```
/events 
```
Instead of :

```
/getEvents
```

- **Use of HTTP methods :** 
We use the HTTP verbs as action in RESTful APIs. We could operate on our URLs with HTTP verbs. 

Those verbs are : **GET, POST, UPDATE, DELETE** .
![httpverbs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2nh4cf4wjqgzfaxocste.png)

## STEP3 : Migrations & Models
Before moving to migration, make sure that you've created a database and added it to the **env** file, like we've done in **step1**. 

Now, let's start by creating our first model and migration--the Event. 
For that, we'll use the following command:

```
$php artisan make:model Event -m
```
**Note:** The **-m** refers to **--migration** and it notifies the Artisan to create one for the model. 

A migration file will be created in the **database/migrations** to generate our table. So, we have to modify the migration file to add the attributes that we're gonna work with. 


![migration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lp1cstr2ra07tll10nf6.PNG)

With that done, let's go ahead and migrate by using the command below: 

```
php artisan migrate
```
Going back to the models part, we'll add the two attributes to the Event model to the `$fillable` field.

📌 **Fillable :** It's a protected property that help us to use a short way to insert data. For example, the create function will look like 

```
Event::create($request->all());
``` 
This is the model file that we'll end up with : 

![model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/880vln22o76hkujnfxjm.PNG)

## STEP 4 : Routes & Controllers

On the `routes/api.php` file, we'll create the essentials endpoints for the application.

![routes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tm7e91guh6zuabuk3rev.PNG)

📌 **Note:** All routes in **api.php** are prefixed with **/api** by default.

Now that we have set up our application, we can proceed to create the controller that will be composed of the methods for our API, so we'll run the following command: 

```
$php artisan make:controller EventController
```
Then we'll add all the functions: 

- 
**Show all the Events:** 

![show](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwxelyaxexcftswh7n3o.PNG)

- 
**Show a specific Event:**

![show2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqv7evhyyw8bnhioy7m8.PNG) 

- 
**Adding an Event:**

![add](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6uwee9j0zvmd87xw5ifk.PNG)

- 
**Updating an Event:**

![update](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a05bew79vnpzfs0pwfek.PNG)

- 
**Deleting an Event:** 

![delete](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ttvgju2qb12l7t36kum.PNG)

**=>** We've used the **_HTTP status_** to indicate whether a request has been successfully completed or not.
There are many HTTP status that we could use, in this blog, i'll explain the ones that have existed in the project: 

**200** = Request succeeded.
**201** = Object created.
**204** = No content.
**404** = Resource Not found. 


## STEP 5 : Testing

Make sure that your server is running before moving to the test phase. 

```
$php artisan serve
```
Then, you could use **POSTMAN** as a test tool for your API. 

__________________________________________________

**Thanks for reading, feedbacks are appreciated.** 🦄