DEV Community

Cover image for Create An API Using Laravel
Hazar nenni
Hazar nenni

Posted on • Edited on

Create An API Using Laravel

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

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
Enter fullscreen mode Exit fullscreen mode

Indeed, you have to set up your database informations in the .env file

database

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 
Enter fullscreen mode Exit fullscreen mode

Instead of :

/getEvents
Enter fullscreen mode Exit fullscreen mode
  • 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

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
Enter fullscreen mode Exit fullscreen mode

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

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

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

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());
Enter fullscreen mode Exit fullscreen mode

This is the model file that we'll end up with :

model

STEP 4 : Routes & Controllers

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

routes

📌 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
Enter fullscreen mode Exit fullscreen mode

Then we'll add all the functions:

  • Show all the Events:

show

  • Show a specific Event:

show2

  • Adding an Event:

add

  • Updating an Event:

update

  • Deleting an Event:

delete

=> 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
Enter fullscreen mode Exit fullscreen mode

Then, you could use POSTMAN as a test tool for your API.


Thanks for reading, feedbacks are appreciated. 🦄

Top comments (0)