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.
Table of Contents
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
- Ensure you have Composer installed on your machine.
- Open your terminal or command prompt.
- Run the following command to create a new Laravel project:
composer create-project --prefer-dist laravel/laravel your-app-name
- Navigate into your project directory:
cd your-app-name
Step 2: Configure Database
- Open the
.env
file located in the root of your Laravel application. - 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
- Create the database in your MySQL server if it does not exist.
Step 3: Create Model and Migration for DB Tables
- Generate a model and migration file using Artisan command:
php artisan make:model YourModelName -m
- Open the migration file located in the
database/migrations
directory. - 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(); }); }
- Run the migration to create the table:
php artisan migrate
Step 4: Add a Resource Route
- Open the
routes/web.php
file. - Add a resource route for your model:
Route::resource('yourmodel', YourModelController::class);
Step 5: Create a Controller
- Generate a resource controller using Artisan command:
php artisan make:controller YourModelController --resource
- Open the newly created controller located in
app/Http/Controllers
. - Implement the CRUD methods (index, create, store, show, edit, update, destroy) according to your requirements.
Step 6: Create Blade Files
- Create a new directory for your views in
resources/views
:resources/views/yourmodel
- 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
- Start the Laravel development server:
php artisan serve
- Open your web browser and navigate to
http://localhost:8000/yourmodel
. - 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.