CRUD Operations in Laravel 12 with GraphQL – A Beginner’s Guide
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!
1. Setting Up Laravel 12 with GraphQL
Step 1: Install Laravel 12
First, create a new Laravel project:
composer create-project laravel/laravel laravel-graphql-crud
cd laravel-graphql-crud
Step 2: Install Lighthouse (GraphQL for Laravel)
Lighthouse is a powerful package that integrates GraphQL with Laravel. Install it via Composer:
composer require nuwave/lighthouse
Step 3: Publish Lighthouse Configuration
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.
2. Creating a Model & Migration
Let’s build a simple "Task" CRUD system.
Step 1: Generate Task Model & Migration
php artisan make:model Task -m
Step 2: Update the Migration File
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();
});
Step 3: Run the Migration
php artisan migrate
3. Defining GraphQL Schema for CRUD
Now, let’s define our GraphQL operations in graphql/schema.graphql.
1. Create a Task (Create)
type Mutation {
createTask(title: String!, description: String): Task @create
}
2. Fetch All Tasks (Read)
type Query {
tasks: [Task!]! @all
}
3. Fetch a Single Task (Read One)
type Query {
task(id: ID! @eq): Task @find
}
4. Update a Task (Update)
type Mutation {
updateTask(id: ID!, title: String, description: String, is_completed: Boolean): Task @update
}
5. Delete a Task (Delete)
type Mutation {
deleteTask(id: ID!): Task @delete
}
6. Define the Task Type
type Task {
id: ID!
title: String!
description: String
is_completed: Boolean!
created_at: String!
updated_at: String!
}
4. Testing CRUD Operations with GraphQL Playground
GraphQL Playground is an interactive tool to test your API.
Access GraphQL Playground
Run your Laravel development server:
php artisan serve
Visit:
http://localhost:8000/graphql-playground
1. Create a Task
mutation {
createTask(title: "Learn GraphQL", description: "Build a Laravel GraphQL API") {
id
title
}
}
2. Fetch All Tasks
query {
tasks {
id
title
is_completed
}
}
3. Fetch a Single Task
query {
task(id: 1) {
id
title
}
}
4. Update a Task
mutation {
updateTask(id: 1, title: "Master GraphQL", is_completed: true) {
id
title
is_completed
}
}
5. Delete a Task
mutation {
deleteTask(id: 1) {
id
title
}
}
5. Conclusion
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! 🚀
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.