Laravel 11 CRUD Application Example Tutorial | websolutioncode.com
Laravel 11 CRUD Application Example Tutorial | websolutioncode.com

Laravel 11 CRUD Application Example Tutorial

Table of Contents:

  1. Introduction
  2. Setting Up Laravel 11
  3. Creating the Database
  4. Building the CRUD Application
    • 4.1 Creating Models
    • 4.2 Creating Controllers
    • 4.3 Creating Views
    • 4.4 Implementing CRUD Operations
  5. Conclusion

1. Introduction:

Laravel is a powerful PHP framework known for its elegant syntax and robust features, making it an excellent choice for building web applications. In this tutorial, we’ll walk through the process of creating a simple CRUD (Create, Read, Update, Delete) application using Laravel 11. CRUD applications are fundamental in web development, allowing users to interact with a database through basic operations.

2. Setting Up Laravel 11:

First, ensure you have PHP and Composer installed on your system. Then, open your terminal and run the following command to create a new Laravel project:

composer create-project laravel/laravel my-crud-app "11.*"

This command will create a new Laravel project named my-crud-app using Laravel 11.

3. Creating the Database:

Next, set up your database configuration in the .env file of your Laravel project. Provide your database credentials such as DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, and DB_PASSWORD.

After configuring your database, run the migration command to create the necessary tables:

php artisan migrate

4. Building the CRUD Application:

4.1 Creating Models: Models represent the data structure of your application. Create a model using the following command:

php artisan make:model Item -m

This command will create a model named Item along with a migration file.

4.2 Creating Controllers:

Controllers handle the logic of your application. Create a controller using the following command:

php artisan make:controller ItemController

4.3 Creating Views:

Views represent the user interface of your application. Create views for your CRUD operations inside the resources/views directory.

For example, you can create index.blade.php, create.blade.php, edit.blade.php, and show.blade.php for listing, creating, updating, and viewing items respectively.

4.4 Implementing CRUD Operations:

Now, let’s implement the CRUD operations in our Laravel application.

  • Creating Records (Create Operation): Create a form in your create.blade.php view to collect data for a new item. Use HTML form elements like <input> and <textarea>.
<form action="{{ route('items.store') }}" method="POST">
    @csrf
    <input type="text" name="name" placeholder="Item Name">
    <textarea name="description" placeholder="Item Description"></textarea>
    <button type="submit">Add Item</button>
</form>

Reading Records

(Read Operation): Display the list of items in your index.blade.php view. Fetch items from the database using Eloquent ORM.

@foreach($items as $item)
    <p>{{ $item->name }}</p>
    <p>{{ $item->description }}</p>
@endforeach

Updating Records

(Update Operation): Create a form in your edit.blade.php view to update an existing item.

<form action="{{ route('items.update', $item->id) }}" method="POST">
    @csrf
    @method('PUT')
    <input type="text" name="name" value="{{ $item->name }}">
    <textarea name="description">{{ $item->description }}</textarea>
    <button type="submit">Update Item</button>
</form>

Deleting Records

(Delete Operation): Implement the delete functionality in your controller.

public function destroy($id)
{
    Item::destroy($id);
    return redirect()->route('items.index')->with('success', 'Item deleted successfully');
}

5. Conclusion:

Congratulations! You’ve successfully built a CRUD application using Laravel 11. CRUD operations are essential for any web application, and Laravel provides a convenient way to implement them efficiently. Feel free to explore more features of Laravel to enhance your application further. Happy coding!

Leave a Reply