In this article I’ll show you how to create API in Laravel in a detailed step by step process.
In Laravel API is mostly used when you work with the mobile applications. API creation is very easy with Laravel because Laravel provide a very easy way to create Rest APIs.
So let’s start the understanding API building in Laravel Step by Step
Steps on how to create API in Laravel
#Step1. Install Laravel
To create API in Laravel we will start from installing Laravel with the help of following command
composer create-project laravel/laravel:^8.0 Laravel
#Step2. Configure the database
In this step on how to create API in Laravel we will create configure the database in our Laravel project. For that we will add the following lines in the .env file which is present in the root folder
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name
DB_USERNAME=here database username
DB_PASSWORD=here database password
Note: Most of the values are given by before in .env
file you just have to update these values
#Step3. Create a table in database
The next step on how to create API in Laravel is to create a table. For that, use below comand
In this step to create our API we will create a table by the following command
php artisan make:migration create_blogs_table
Now open migration file of your project and update the following code
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateBlogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('blogs', function (Blueprint $table) {
$table->id();
$table->string('title', 255)->nullable();
$table->text('body')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('blogs');
}
}
Now to create table you have to run the migration file by the following command
php artisan migrate
This will create a table in your database by the name of create_blogs_table.
#Step4. Adding Resource Route
In this step on how to create API in Laravel we need to add a resource route for CRUD operations
Now, open routes/api.php
file and add the following route
use App\Http\Controllers\BlogController;
Route::resource('blogs', BlogController::class);
#Step5. Adding Model and Controller
In this step on how to create API in Laravel, we will now add model and controller by the help of following command
php artisan make:controller BlogController --resource –model=Blog
After you enter the command given above, you will find a new file in the below path
app/Http/Controllers/BlogController.php
Now, in this controller we will create 7 methods by default as shown below:
- index()
- create()
- store()
- show()
- edit()
- update()
- destroy()
Now go to the project path: app/Http/Controllers/BlogController.php
And update the code as follows
<?php
namespace App\Http\Controllers;
use App\Models\Blog;
use Illuminate\Http\Request;
class BlogController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$blogs = Blog::latest()->paginate(10);
return [
"status" => 1,
"data" => $blogs
];
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'body' => 'required',
]);
$blog = Blog::create($request->all());
return [
"status" => 1,
"data" => $blog
];
}
/**
* Display the specified resource.
*
* @param \App\Models\Blog $blog
* @return \Illuminate\Http\Response
*/
public function show(Blog $blog)
{
return [
"status" => 1,
"data" =>$blog
];
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Blog $blog
* @return \Illuminate\Http\Response
*/
public function edit(Blog $blog)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Blog $blog
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Blog $blog)
{
$request->validate([
'title' => 'required',
'body' => 'required',
]);
$blog->update($request->all());
return [
"status" => 1,
"data" => $blog,
"msg" => "Blog updated successfully"
];
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Blog $blog
* @return \Illuminate\Http\Response
*/
public function destroy(Blog $blog)
{
$blog->delete();
return [
"status" => 1,
"data" => $blog,
"msg" => "Blog deleted successfully"
];
}
}
#Step6. Run CRUD project
In this final step of how to create API in Laravel tutorial, now we will run the CRUD application using the command below
php artisan serve
#Step7. Testing
We have successfully created Laravel API. Now we can open the below URL on Postman:
1. Create new Blog Article
url: http://127.0.0.1:8000/blogs
method: POST
data: { title: “Title”, body: “Body here..” }
2. Update any Blog Article
url: http://127.0.0.1:8000/blogs/{id}
method: PUT/PATCH
data: { title: “Update Title”, body: “Update Body here..” }
3. Get All of the Blogs
url: http://127.0.0.1:8000/blogs/
method: GET
4. Get any Single Blog
url: http://127.0.0.1:8000/blogs/{id}
method: GET
5. Delete any Blog
url: http://127.0.0.1:8000/blogs/{id}
method: DELETE
Conclusion
Using the steps outlined above, you can Create API in Laravel. If you have any questions about this Laravel 8 API tutorial, please leave them in the comments section. Alternatively, you can hire a Laravel Developer to assist you in creating a Laravel API.
If you found this how to create API in Laravel tutorial useful, please share it with your friends and stay tuned for more updates.
How do I create API document in Laravel?
Use the apidoc:generate artisan command to create your API documentation. It will create documentation based on the configuration you enter. The created documentation will be stored in the selected output folder as static HTML and CSS assets.
Is Laravel good for API?
Laravel lets you build API very easily and quickly. In laravel it can be the back-end to a front-end web app, a data source for a mobile app, or a service for other apps or APIs.