Create Restful API Using Laravel
Hi, I want to share you about how to creating a Restful API service using Laravel. I used “todo list case” for this tutorial, where we have activities and we have more items for an activity.
Our step by step in this tutorial
- Create new Laravel project
- Setup database migration
- Set the API endpoint and work on the backend
- Test using Postman
Step 1 — Create new Laravel project
Create our new Laravel project by typing this in our terminal:
composer create-project — prefer-dist laravel/laravel todolist
After that, change directory into todolist project directory.
Step 2 — Setup database migration
Before running the migration, make sure we already created the database, so we can set up our .env file first. Just fill the database setup correctly, this is my .env file setup for example:
After that, we can create a new table for our this project. In todo list case, we need activities table and items table, where the relations of both is one to many, one activity can have more items.
Now, we’ll create activities table migration, we can start with type something like this:
php artisan make:migration create_activities_table — create=activities
we can look at database/migrations folder, there is migration file we are created before, like this:
Then we’ll create items table migration, with typing like this:
php artisan make:migration create_items_table — create=items
We are now finish with migrations setup, let’s running that with typing:
php artisan migrate
After that, we can look at our database, there is some tables like this:
Now, our database setup is ready, continue to the next step.
Step 3 — Set the API endpoint and work on the controller
We can setup our routes/api.php file became like this:
Above is our API endpoint setup, I assume you are already experienced with Laravel routes, so I thing the route above is not so hard to understand. After created the routes, now it’s time to create a function in the controller.
We are create each function declared in route in the controller, this is our controller:
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;
use App\Activity;
use App\Item;class ActivitiesController extends Controller
{
public function store(Request $request)
{
$activity_name = $request->input('activity_name');$data = array(
'activity_name' => $activity_name
);$activity = Activity::create($data);if ($activity) {
return response()->json([
'data' => [
'type' => 'activities',
'message' => 'Success',
'id' => $activity->id,
'attributes' => $activity
]
], 201);
} else {
return response()->json([
'type' => 'activities',
'message' => 'Fail'
], 400);
}
}public function storeLists(Request $request, $activity_id)
{
$item_name = $request->input('item_name');$item = Item::create([
'item_name' => $item_name,
'activity_id' => $activity_id,
'status' => 0
]);if ($item) {
return response()->json([
'data' => [
'type' => 'activity items',
'message' => 'Success',
'id' => $item->id,
'attributes' => $item
]
], 201);
} else {
return response()->json([
'type' => 'activity_items',
'message' => 'Fail'
], 400);
}
}public function show()
{
$activities = Activity::with('items')->get();return response()->json([
'data' => $activities
], 200);
}public function activityUpdate(Request $request, $activity_id)
{
$activity = Activity::find($activity_id);if ($activity) {
$activity->activity_name = $request->input('activity_name');
$activity->save();return response()->json([
'data' => [
'type' => 'activities',
'message' => 'Update Success',
'id' => $activity->id,
'attributes' => $activity
]
], 201);
} else {
return response()->json([
'type' => 'activities',
'message' => 'Not Found'
], 404);
}
}public function itemUpdate(Request $request, $activity_id, $item_id)
{
$item = Item::where('activity_id', $activity_id)->where('id', $item_id)->first();if ($item) {
$item->item_name = $request->input('item_name');
$item->status = $request->input('status');
$item->save();return response()->json([
'data' => [
'type' => 'items',
'message' => 'Update Success'
]
], 201);
} else {
return response()->json([
'type' => 'items',
'message' => 'Not Found'
], 404);
}
}public function getActivityById($activity_id)
{
$activity = Activity::with('items')->find($activity_id);if ($activity) {
return response()->json([
'data' => [
'type' => 'activities',
'message' => 'Success',
'attributes' => $activity
]
], 200);
} else {
return response()->json([
'type' => 'activities',
'message' => 'Not Found'
], 404);
}
}public function activityDestroy($activity_id)
{
$activity = Activity::find($activity_id);if ($activity) {
$activity->delete();return response()->json([], 204);
} else {
return response()->json([
'type' => 'activities',
'message' => 'Not Found'
], 404);
}
}public function activityItemDestroy($activity_id, $item_id)
{
$item = Item::where('activity_id', $activity_id)->where('id', $item_id)->first();if ($item) {
$item->delete();return response()->json([], 204);
} else {
return response()->json([
'type' => 'items',
'message' => 'Not Found'
], 404);
}
}
}
The last step is test the API service using Postman, let’s go..
Step 4 — Test using postman
Please look at my github repository, there is endpoint you can try to call using postman,
I will try to call this API endpoint https://lit-atoll-41704.herokuapp.com/api/v1/activities
with GET request to get all activities data. The result is like this:
Finally, our API is ready to use, hopefully this tutorial is help you all guys, thanks for reading :)