Make REST API in LARAVEL 9
Today I am going to explain how you can make Rest Api in Laravel 9. In this video, I am going to explain about CRUD Operation using REST API.
In this post, we’ll show you how to build a REST API with laravel 9. Before you can proceed with this tutorial, you need to install the latest version of laravel, and make sure you have the following prerequisites:
- PHP 8.0.2+
- MySql
- Composer
Laravel 9 is a full-stack web application framework with a robust and visually appealing design that is easy to use to create traditional web applications and modern web applications that expose REST APIs that could be consumed by front-end applications such as Angular, React or Vue.js.
Using a web framework as a basis and starting point for developing your application will allow you to focus on creating something really amazing while the foundation takes care of the low-level technicalities for you.
You can also use laravel 9 to build a REST API that can be consumed by a JavaScript client application.
Laravel 9 Installation.
After installing laravel9 we will open the code in Editor.
Step1: Create a new laravel 9 model
Open a new command line interface and run the following command from the project root folder to generate a product model with a migration file:
php artisan make:model Post -m
Next, open the migration file inside the database/migrations folder and update it as follows:
id();
$table->string('title');
$table->longText('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};
Following that, open the app/models/Post.php file and update the fillable array as follows:
Step 2: Create the REST API controller
After creating the model and migration file for products, let’s now create the controller. Head back to your command-line interface and run the following command:
php artisan make:controller Api\\PostController --model=Post
Next, open the app/Http/Controllers/Api/PostController.php file and update the code below
json([
'status' => true,
'posts' => $posts
]);
}
/**
* 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(StorePostRequest $request)
{
$post = Post::create($request->all());
return response()->json([
'status' => true,
'message' => "Post Created successfully!",
'post' => $post
], 200);
}
/**
* Display the specified resource.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function show(Post $post)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function edit(Post $post)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function update(StorePostRequest $request, Post $post)
{
$post->update($request->all());
return response()->json([
'status' => true,
'message' => "Post Updated successfully!",
'post' => $post
], 200);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
$post->delete();
return response()->json([
'status' => true,
'message' => "Post Deleted successfully!",
], 200);
}
}
Step 3: Request to validate data
Now Let’s create the request to validate the data by running the command below.
php artisan make:request StorePostRequest
Now open the file from app/Http/Requests/StorePostRequest.php and update the code below.
"required|max:70",
"description" => "required"
];
}
}
Step 4: Create the REST API endpoints
Conclusion
We’ve seen how to install laravel 9 by creating a new project using composer. In order to provide an amazing developer experience, Laravel includes features like robust dependency injection, a powerful database abstraction layer, queues and scheduled jobs, unit and integration testing, and more.
Laravel is a framework that can adapt to your skills, whether you are a beginner to PHP web frameworks or have years of expertise. Laravel 9 assists you in taking your initial steps as a web developer or gives you a push as you advance your skills.
The post How to build REST API with Laravel 9? appeared first on Magespider Solutions.