Building a CRUD Application with Laravel 11: A Comprehensive Guide with Code Examples

author

By Freecoderteam

May 02, 2024

342

image

Laravel remains one of the most popular PHP frameworks due to its ease of use, extensive community support, and robust features that accelerate web application development. In this blog post, we'll delve into creating a basic CRUD (Create, Read, Update, Delete) application using Laravel 11. This guide will cover everything from setting up your Laravel environment to writing the actual CRUD operations.

Prerequisites

Before we start, make sure you have the following installed on your system:

  • PHP (version 8.0 or higher)
  • Composer
  • A relational database (MySQL, PostgreSQL, etc.)

Step 1: Install Laravel 11

Create a new Laravel project named "blog" by running the following command in your terminal:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Set Up Environment

Configure your .env file within your project to connect to your database:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_blog
DB_USERNAME=root
DB_PASSWORD=

Step 3: Create Model and Migration

Create a model called Post along with a migration file for the posts table:

php artisan make:model Post -m

Edit the migration file in database/migrations to add fields to your posts table:

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->text('body');
    $table->timestamps();
});

Run the migration:

php artisan migrate

Step 4: Create Controller

Generate a controller named PostController:

php artisan make:controller PostController

In the controller, implement the CRUD methods:

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::all();
        return view('posts.index', compact('posts'));
    }

    public function create()
    {
        return view('posts.create');
    }

    public function store(Request $request)
    {
        Post::create($request->all());
        return redirect()->route('posts.index');
    }

    public function show(Post $post)
    {
        return view('posts.show', compact('post'));
    }

    public function edit(Post $post)
    {
        return view('posts.edit', compact('post'));
    }

    public function update(Request $request, Post $post)
    {
        $post->update($request->all());
        return redirect()->route('posts.index');
    }

    public function destroy(Post $post)
    {
        $post->delete();
        return redirect()->route('posts.index');
    }
}

Step 5: Define Routes

Add resourceful routes to handle CRUD operations in routes/web.php:

Route::resource('posts', PostController::class);

Step 6: Create Views

You'll need to create views for each of the CRUD operations. Here’s an example of a simple form for creating a post (resources/views/posts/create.blade.php):

<form action="{{ route('posts.store') }}" method="POST">
    @csrf
    <label for="title">Title:</label>
    <input type="text" name="title" id="title" required>

    <label for="body">Content:</label>
    <textarea name="body" id="body" required></textarea>

    <button type="submit">Create Post</button>
</form>

Conclusion

With these steps, you've set up a basic CRUD application in Laravel 11. This guide provides a foundational understanding of handling data with Laravel and gives you a solid base to expand upon with more complex functionalities and features. Laravel's MVC architecture makes it an excellent choice for building scalable and maintainable web applications.

Popular Tags :
Share this post :

Subscribe to Receive Future Updates

Stay informed about our latest updates, services, and special offers. Subscribe now to receive valuable insights and news directly to your inbox.

No spam guaranteed, So please don’t send any spam mail.