CRUD Operations in Laravel 12 with GraphQL – A Beginner’s Guide

Author

Kritim Yantra

Apr 11, 2025

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! 🚀

Tags

Laravel GraphQL

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Continue with Google

Related Posts

Laravel 12 Roles and Permissions Setup: Complete Guide
Kritim Yantra Kritim Yantra
Feb 28, 2025
Laravel 12 & AdminLTE Integration: Setup Your Stunning Admin Dashboard
Kritim Yantra Kritim Yantra
Feb 28, 2025
What Are Laravel 12 Service Providers?
Web Development
What Are Laravel 12 Service Providers?
Laravel Vue
Kritim Yantra Kritim Yantra
Mar 02, 2025