How to make API on Laravel
Build a Laravel API: Learn how to create an API in Laravel with an example.
Creating an API with Laravel
This tutorial will show you how to make a functional API using Laravel, a popular PHP framework. We'll be using the latest version of Laravel, version 7.0, for this tutorial. It's important to note that Laravel 7.0 requires PHP 7.2.5 or higher, so make sure you have that installed before beginning.
Let's start by creating a new Laravel application. You can do this using the Laravel installer, or you can use the following command in your terminal:
composer create-project --prefer-dist laravel/laravel my-api
This will create a new Laravel application with the name my-api
, and it will be located in a directory of the same name. You can change the name of the directory if you'd like, but for the sake of this tutorial we'll use the same name.
Once the application has been created, you'll need to set up your database. You can do this by editing the .env
file in the root of your application. You'll need to provide your database credentials and set the database type to mysql
.
Now that your database is set up, you'll need to create your API routes. You can do this by creating a routes/api.php
file in the root of your application. In this file, you can define your API routes and the associated controller functions.
Route::get('/users', 'UserController@index');
Route::post('/users', 'UserController@store');
Route::get('/users/{id}', 'UserController@show');
Route::put('/users/{id}', 'UserController@update');
Route::delete('/users/{id}', 'UserController@delete');
These routes will allow you to perform basic CRUD operations on your users. You can add more routes as needed, such as for authentication or other API endpoints.
Next, you'll need to create the associated controllers. You can do this by running the following command in your terminal:
php artisan make:controller UserController
This command will create a new controller file in the app/Http/Controllers
directory. You'll need to open this file and add the methods for each of your API routes. For example, you can add the following methods to your UserController
:
public function index()
{
// Return all users
}
public function store(Request $request)
{
// Create a new user
}
public function show($id)
{
// Return a single user
}
public function update(Request $request, $id)
{
// Update an existing user
}
public function delete($id)
{
// Delete an existing user
}
You can add logic to each of these methods to handle your API requests. For example, in the index
method you would query your database for all users and return them as a JSON response. In the store
method you would create a new user based on the data in the request.
Once you've created your controllers, you'll need to set up your database migrations. You can do this by running the following command in your terminal:
php artisan make:migration create_users_table
This command will create a new migration file in the database/migrations
directory. You'll need to open this file and add the necessary fields for your users. For example, you could add the following fields:
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
Once you've added the fields, you can run the migrate command to create your users table:
php artisan migrate
Now that your database is set up, you'll need to create your models. You can do this by running the following command in your terminal:
php artisan make:model User
This command will create a new model file in the app
directory. You'll need to open this file and add the necessary relationships and methods for your users. For example, you could add the following relationships:
public function posts()
{
return $this->hasMany(Post::class);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
And you could add the following methods:
public function getNameAttribute()
{
return ucfirst($this->name);
}
public function getEmailAttribute()
{
return strtolower($this->email);
}
Once you've added the necessary logic to your models, you can use them in your controllers to handle your API requests. For example, you can use the User
model in your UserController
to query the database and return the necessary data.
And that's it! You now have a working API with Laravel. You can add more routes, controllers, models, and migrations as needed to build out your API. Good luck, and happy coding!