1 HOUR SPECIAL - LARAVEL 11 & FILAMENT CRUD DASAR

4 min read 4 hours ago
Published on Nov 25, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

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.

  1. Install Composer: Ensure you have Composer installed on your machine. This is essential for managing PHP dependencies.

  2. 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
      
  3. Install Filament:

    • Run the following command to install Filament:
      composer require filament/filament
      

Step 2: Configure Your Database

Before you can utilize Laravel's features, you need to set up your database.

  1. Create a Database: Use your preferred database management tool (like phpMyAdmin) to create a new database for your application.

  2. 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
      

Step 3: Create a Model and Migration

Next, create a model and migration file for the data you will manage.

  1. 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 the app/Models directory and a migration file in the database/migrations directory.
  2. 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();
          });
      }
      
  3. Run Migration:

    • Apply the migration by running:
      php artisan migrate
      

Step 4: Implement CRUD Functionality

Now, you will implement the CRUD operations.

  1. Create a Controller:

    • Generate a controller for handling CRUD operations:
      php artisan make:controller ItemController
      
  2. Define Routes:

    • Open the routes/web.php file and add the following routes:
      Route::resource('items', ItemController::class);
      
  3. 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.');
      }
      

Step 5: Create Views

Create the necessary views for your CRUD operations.

  1. Create Blade Templates:

    • In the resources/views directory, create a folder named items.
    • 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.
  2. 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
      

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.