Laravel 11 CRUD Application Example

3 min read 14 days ago
Published on May 07, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Introduction

In this tutorial, you will learn how to create a simple CRUD (Create, Read, Update, Delete) application using Laravel 11. CRUD operations are fundamental for any web application that requires database interaction. This guide will walk you through the essential steps to set up your application, configure your database, and implement CRUD functionality effectively.

Step 1: Install Laravel and Create a New Application

  1. Ensure you have Composer installed on your machine.
  2. Open your terminal or command prompt.
  3. Run the following command to create a new Laravel project:
    composer create-project --prefer-dist laravel/laravel your-app-name
    
  4. Navigate into your project directory:
    cd your-app-name
    

Step 2: Configure Database

  1. Open the .env file located in the root of your Laravel application.
  2. Update the database configuration settings:
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=your_database_name
    DB_USERNAME=your_database_user
    DB_PASSWORD=your_database_password
    
  3. Create the database in your MySQL server if it does not exist.

Step 3: Create Model and Migration for DB Tables

  1. Generate a model and migration file using Artisan command:
    php artisan make:model YourModelName -m
    
  2. Open the migration file located in the database/migrations directory.
  3. Define the schema for your table in the up method:
    public function up()
    {
        Schema::create('your_table_name', function (Blueprint $table) {
            $table->id();
            $table->string('column_name');
            $table->timestamps();
        });
    }
    
  4. Run the migration to create the table:
    php artisan migrate
    

Step 4: Add a Resource Route

  1. Open the routes/web.php file.
  2. Add a resource route for your model:
    Route::resource('yourmodel', YourModelController::class);
    

Step 5: Create a Controller

  1. Generate a resource controller using Artisan command:
    php artisan make:controller YourModelController --resource
    
  2. Open the newly created controller located in app/Http/Controllers.
  3. Implement the CRUD methods (index, create, store, show, edit, update, destroy) according to your requirements.

Step 6: Create Blade Files

  1. Create a new directory for your views in resources/views:
    resources/views/yourmodel
    
  2. Create Blade files for each view required (index, create, edit)
    • index.blade.php: Display a list of records.
    • create.blade.php: Form for creating a new record.
    • edit.blade.php: Form for editing an existing record.

Step 7: Run and Test Your CRUD Application

  1. Start the Laravel development server:
    php artisan serve
    
  2. Open your web browser and navigate to http://localhost:8000/yourmodel.
  3. Test all CRUD operations to ensure everything functions correctly.

Conclusion

You have successfully built a basic CRUD application using Laravel 11. This tutorial covered the essential steps from installation to testing your application. Next, consider enhancing your app by adding validation, authentication, or deploying it to a web server for broader access. Keep exploring Laravel's features to build more complex applications.