This guide will walk you through the process of creating a REST API using Laravel, a popular PHP framework. Before diving in, make sure you are familiar with MVC frameworks. You can find more information here.
Before diving to API lets discuss it in a simple or non program type of language, Imagine an API like a menu in a restaurant. You, the client, order from the menu, and the server, or the backend, prepares your request. The API is the middleman that facilitates this communication, just like how a menu helps you communicate your order to the kitchen. It's a way for different software systems to talk to each other effectively and efficiently.
Make sure you have the following set up:
- Laravel Installed
- Visual Studio Code or any IDE of your choice
- Postman
cd your_laravel_folder
Before hopping to the code what is some shortcut in creating. This is the shortcuts mostly we will be using
-m = migration
-f = factory
-s = seeder
-c = controller
-a = all at once
php artisan make:model NameofModel -mfs
After pressing Enter in that it will be able to create a Model with the Migration, Factories, and Seeder Files of that model.
php artisan make:controller NameofController --api --model=NameofModel
php artisan make:resource NameofResource
php artisan make:resource NameofCollection
Controller
use App\Http\Resources\ProjectPartnerResource;
use App\Http\Resources\ProjectPartnerCollection;
use Illuminate\Http\Response;
or
Index is the one getting the response or the data
public function index()
{
return response()->json(new ProjectPartnerCollection
(ProjectPartner::all()), Response::HTTP_OK);
}
Show is for displaying specified Resources or viewing some of the information
public function show(ProjectPartner $projectPartner)
{
return new ProjectPartnerResource($projectPartner);
}
Store Function the data inside are what supposed to be inside your table
public function store(Request $request)
{
$projectPartner = ProjectPartner::create($request->only([
'scholarship_categ_id',
'project_partner_name',
'project_partner_mobile_num',
'school_id',
]));
return new ProjectPartnerResource($projectPartner);
}
Updating the displayed data and editing the information in it
Remember here: I Call the $id its not default by Laravelpublic function update(Request $request, ProjectPartner $projectPartner, $id)
{
$projectPartner = ProjectPartner::find($id);
$projectPartner->update($request->all());
return new ProjectPartnerResource($projectPartner);
}
Deleting the Resource or the Data in it also in here $id is not default
public function destroy(ProjectPartner $projectPartner, $id)
{
$deleted = ProjectPartner::destroy($id);
if($deleted === 0) {
return response()->json(['message' => 'Project Partner not found'], 404);
} elseif ($deleted === null) {
return response()->json(['message' => 'Error deleting project partner'], 500);
}
return response()->json(['message' => 'Project Partner deleted'], 200);
}
Now were done in our Controller File now lets hop in our Model Part
So, the Model will be the one handling the columns in your Table or in the Database
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ProjectPartner extends Model
{
use HasFactory;
protected $fillable = [
'scholarship_categ_id',
'project_partner_name',
'project_partner_mobile_num',
'school_id',
];
}
Now were done in our Model File now lets hop in our Resources and Collection Part
Step 7: Now we will be creating a parent array for our data, so we will put this code in the Collection we created
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
class ProjectPartnerCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
return [
'data' => $this->collection,
];
}
}
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'scholarship_categ_id' => $this->scholarship_categ_id,
'project_partner_name' => $this->project_partner_name,
'project_partner_mobile_num' => $this->project_partner_mobile_num,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'deleted_at' => $this->deleted_at,
'school_id' => $this->school_id,
];
}
Now were done in our Resources and Collection File now lets hop in our Migration Files Part
Step 9: Here we create type the column that the database should have so this will be the column present in the database all of it
public function up(): void
{
Schema::create('project_partners', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('scholarship_categ_id');
$table->string('project_partner_name');
$table->string('project_partner_mobile_num');
$table->timestamps();
});
}
Now were done in our Migration File now lets hop in our Seeder Files Part
Step 10: Now lets hop on the seeders so we will be matching the data needed or the columns we put in our table. Below is the example of data im providing or inserting in the table
DB::table('project_partners')->insert([
[
'project_partner_name' => 'BACPAT Youth Development Foundation Inc.',
'project_partner_mobile_Num' => '09011223344'
],
[
'project_partner_name' => 'Welcome Home Foundation Inc. (BACPAT Scholars)',
'project_partner_mobile_Num' => '09055667788'
],
[
'project_partner_name' => 'Education for Former OSY',
'project_partner_mobile_Num' => '09098765432'
],
[
'project_partner_name' => 'Bahay Maria Children Center Formal',
'project_partner_mobile_Num' => '09011112222'
],
[
'project_partner_name' => 'Canossian Sisters – Endorsed Grantees c/o Sr. Elizabeth Tolentino',
'project_partner_mobile_Num' => '09033334444'
],
[
'project_partner_name' => 'Canossian Sisters – Endorsed Grantees c/o Sr. Elizabeth Tolentino & Sr. Mila Reyes',
'project_partner_mobile_Num' => '09055556666'
],
// ... (add other partner details in the same format)
]);
Step 11: Now to create an API Endpoint for our Laravel is we need to modify the API.php in the routes folder of Laravel
Things to Remember before modifying API.php
- Remember to import your controller at the top of api.php. In our case, we need to import ProjectPartnerController at the top.
- Then let's create our method functions for handling every type of data. This includes GET, PUT, POST, and DELETE.
So before we create the code in Routes lets dicuss first the Format Structure of Routing. For now we use GET Method to discuss Format Structure
Route::apiResource('/project-partners', ProjectPartnerController::class)->only(['index', 'show']);
- Route: This will be the word to use for routing or API, and it will be the default in the API.php file. This will be used in every method.
- apiResource: This is used to specify the method we will be using. We can use get, put, post, and delete here.
- The "/project-partner": This will be the endpoint we will be calling in Postman, and we can modify this to suit our needs.
- ProjectPartnerController: This will be the controller file name and will serve as the basis for where our functions will be running. This is where we put our index, show, and update functions, based on your code.
- "index" and "show": These can be modified by you, but in my case, I created my function names as index and show, basing them on the names of functions.
So in my end i create my endpoints and function in this format
Route::apiResource('/project-partners', ProjectPartnerController::class)->only(['index', 'show']);
Route::post('/project-partners', [ProjectPartnerController::class, 'store']);
Route::put('/project-partners/{id}', [ProjectPartnerController::class, 'update']);
Route::delete('/project-partners/{id}', [ProjectPartnerController::class, 'destroy']);
- GET method (apiResource): Used for retrieving or showing data.
- POST method: Stores new data in our table.
- PUT method: Updates existing data in our table, typically using an ID to specify which entry to modify.
- DELETE method: Removes data from our table.
Now that we completed configuring the file lets go back to our terminal and folder of the backend
127.0.0.1 or it depends to you
php artisan migrate
So, it will be looking like this
In the Database your using you should see this now
Step 14: Now we run the seeder of the table so we we need to import this first in the top of our seeder or else it will return an error of
Class "Database\Seeders\DB" not Found
php artisan db:seed --class=ProjectPartnerSeeder
Step 16: Then now access the API you created and make sure your server is working it should look like this and same with other method function
It would look like this in POSTMAN when you do the GET method
Note that if you will do POST you will be going to the Body and add the Columns needed and if PUT is you will use the ID and add it in the API Endpoint same with the Delete
You can copy and paste this markdown code directly into your README.md file. Make sure to replace placeholders like NameofModel
, NameofController
, etc., with your actual names. Also, adjust image URLs to point to your actual image locations.
If you have any feedback, please reach out to me at [email protected]