Kritim Yantra
Apr 11, 2025
Building a CRUD (Create, Read, Update, Delete) application is a fundamental skill for any developer. In this guide, we’ll use Laravel 12 (the latest version of the popular PHP framework) and GraphQL (a flexible API query language) to create a simple yet powerful API.
By the end of this tutorial, you’ll understand:
✔ How to set up GraphQL in Laravel 12
✔ How to perform CRUD operations using GraphQL
✔ How to test your API with GraphQL Playground
Let’s dive in!
First, create a new Laravel project:
composer create-project laravel/laravel laravel-graphql-crud
cd laravel-graphql-crud
Lighthouse is a powerful package that integrates GraphQL with Laravel. Install it via Composer:
composer require nuwave/lighthouse
Run the following command to generate the default configuration:
php artisan vendor:publish --provider="Nuwave\Lighthouse\LighthouseServiceProvider"
This will create a graphql/schema.graphql
file where we define our API structure.
Let’s build a simple "Task" CRUD system.
php artisan make:model Task -m
Open database/migrations/xxxx_create_tasks_table.php
and define the schema:
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('description')->nullable();
$table->boolean('is_completed')->default(false);
$table->timestamps();
});
php artisan migrate
Now, let’s define our GraphQL operations in graphql/schema.graphql
.
type Mutation {
createTask(title: String!, description: String): Task @create
}
type Query {
tasks: [Task!]! @all
}
type Query {
task(id: ID! @eq): Task @find
}
type Mutation {
updateTask(id: ID!, title: String, description: String, is_completed: Boolean): Task @update
}
type Mutation {
deleteTask(id: ID!): Task @delete
}
type Task {
id: ID!
title: String!
description: String
is_completed: Boolean!
created_at: String!
updated_at: String!
}
GraphQL Playground is an interactive tool to test your API.
Run your Laravel development server:
php artisan serve
Visit:
http://localhost:8000/graphql-playground
mutation {
createTask(title: "Learn GraphQL", description: "Build a Laravel GraphQL API") {
id
title
}
}
query {
tasks {
id
title
is_completed
}
}
query {
task(id: 1) {
id
title
}
}
mutation {
updateTask(id: 1, title: "Master GraphQL", is_completed: true) {
id
title
is_completed
}
}
mutation {
deleteTask(id: 1) {
id
title
}
}
Congratulations! 🎉 You’ve successfully built a CRUD API in Laravel 12 using GraphQL. Here’s what we covered:
✅ Setting up Laravel 12 with GraphQL (Lighthouse)
✅ Creating a Task model & migration
✅ Defining GraphQL queries & mutations
✅ Testing CRUD operations in GraphQL Playground
This setup is perfect for SPAs (Single Page Applications), mobile apps, and APIs where flexibility in data fetching is crucial.
Happy coding! 🚀
Transform from beginner to Laravel expert with our personalized Coaching Class starting June 10, 2025. Limited enrollment ensures focused attention.
1-hour personalized coaching
Build portfolio applications
Industry-standard techniques
Interview prep & job guidance
Complete your application to secure your spot
Thank you for your interest in our Laravel mentorship program. We'll contact you within 24 hours with next steps.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google