1 HOUR SPECIAL - LARAVEL 11 & FILAMENT CRUD DASAR
Table of Contents
Introduction
This tutorial provides a comprehensive guide to creating a basic CRUD (Create, Read, Update, Delete) application using Laravel 11 and Filament. It is designed for beginner programmers looking to enhance their skills with these powerful tools. By the end of this tutorial, you will have a functional CRUD application and a foundational understanding of Laravel and Filament.
Step 1: Setting Up Your Environment
To start building your Laravel application, you need to set up your development environment.
-
Install Composer: Ensure you have Composer installed on your machine. This is essential for managing PHP dependencies.
-
Create New Laravel Project:
- Open your terminal.
- Run the following command to create a new Laravel project:
composer create-project --prefer-dist laravel/laravel your-project-name
- Navigate into your project directory:
cd your-project-name
-
Install Filament:
- Run the following command to install Filament:
composer require filament/filament
- Run the following command to install Filament:
Step 2: Configure Your Database
Before you can utilize Laravel's features, you need to set up your database.
-
Create a Database: Use your preferred database management tool (like phpMyAdmin) to create a new database for your application.
-
Update
.env
File:- Open the
.env
file in your project root. - Update the following lines with your database credentials:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database_name DB_USERNAME=your_username DB_PASSWORD=your_password
- Open the
Step 3: Create a Model and Migration
Next, create a model and migration file for the data you will manage.
-
Generate Model and Migration:
- Run the following command to create a model and a corresponding migration:
php artisan make:model Item -m
- This command creates
Item.php
in theapp/Models
directory and a migration file in thedatabase/migrations
directory.
- Run the following command to create a model and a corresponding migration:
-
Define Migration Schema:
- Open the migration file created in the previous step.
- Update the
up
method to define the table structure:public function up() { Schema::create('items', function (Blueprint $table) { $table->id(); $table->string('name'); $table->text('description'); $table->timestamps(); }); }
-
Run Migration:
- Apply the migration by running:
php artisan migrate
- Apply the migration by running:
Step 4: Implement CRUD Functionality
Now, you will implement the CRUD operations.
-
Create a Controller:
- Generate a controller for handling CRUD operations:
php artisan make:controller ItemController
- Generate a controller for handling CRUD operations:
-
Define Routes:
- Open the
routes/web.php
file and add the following routes:Route::resource('items', ItemController::class);
- Open the
-
Implement Controller Methods:
- Open
ItemController.php
and implement the necessary methods for CRUD:- index: Retrieve all items.
- create: Show the form for creating a new item.
- store: Save a new item to the database.
- edit: Show the form for editing an existing item.
- update: Update an existing item.
- destroy: Delete an item.
- Example for the
store
method:public function store(Request $request) { $request->validate([ 'name' => 'required', 'description' => 'required', ]); Item::create($request->all()); return redirect()->route('items.index')->with('success', 'Item created successfully.'); }
- Open
Step 5: Create Views
Create the necessary views for your CRUD operations.
-
Create Blade Templates:
- In the
resources/views
directory, create a folder nameditems
. - Create the following Blade files:
index.blade.php
: List all items.create.blade.php
: Form to create a new item.edit.blade.php
: Form to edit an existing item.
- In the
-
Implement Blade Logic:
- Use Blade syntax to loop through items and create forms. For example, in
index.blade.php
:@foreach ($items as $item) <tr> <td>{{ $item->name }}</td> <td>{{ $item->description }}</td> <td> <a href="{{ route('items.edit', $item->id) }}">Edit</a> <form action="{{ route('items.destroy', $item->id) }}" method="POST"> @csrf @method('DELETE') <button type="submit">Delete</button> </form> </td> </tr> @endforeach
- Use Blade syntax to loop through items and create forms. For example, in
Conclusion
You have now created a basic CRUD application using Laravel 11 and Filament. You've set up your environment, configured your database, created a model and migration, implemented CRUD functionality, and created views.
Next Steps
- Explore more advanced features of Laravel, such as authentication and validation.
- Customize your application using Filament's UI components.
- Experiment with additional packages to enhance functionality.
Continue to practice and build upon what you've learned to solidify your programming skills.