DEV Community

Dhiecoderweb
Dhiecoderweb

Posted on Originally published at dhiecoderweb.com

How to Build Complete CRUD in Laravel (Beginners)

CRUD stands for Create, Read, Update, Delete — the four basic operations found in almost every web application. In Laravel, building CRUD can be done very efficiently thanks to features like the Eloquent ORM, Resource Controllers, and Blade templating. This article will guide you through building complete CRUD from scratch using Laravel 10/11.

Initial Setup

Make sure you have installed Laravel and set up a database. Create a new project if you don't have one yet:

composer create-project laravel/laravel crud-demo
cd crud-demo
cp .env.example .env
php artisan key:generate
Enter fullscreen mode Exit fullscreen mode

Configure the database connection in the .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=crud_demo
DB_USERNAME=root
DB_PASSWORD=
Enter fullscreen mode Exit fullscreen mode

Creating the Migration and Model

We will build a CRUD feature for product data. Run the following command to create the model and its migration at once:

php artisan make:model Product -m
Enter fullscreen mode Exit fullscreen mode

Open the newly created migration file in the database/migrations/ folder and add the columns you need:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('description')->nullable();
            $table->decimal('price', 10, 2);
            $table->integer('stock')->default(0);
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('products');
    }
};
Enter fullscreen mode Exit fullscreen mode

Run the migration:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

This is only part of the article. For the full discussion, with examples and step-by-step details, you can read it on the original source:

How to Build Complete CRUD in Laravel (Beginners)

More programming tutorials and developer tools are also available at dhiecoderweb.com.

Top comments (0)