Home > Web Development > Laravel Tutorials > Add products to the cart in Laravel 11 easily.

Add products to the cart in Laravel 11 easily.

Diego Cortés
Diego Cortés
January 19, 2025
Add products to the cart in Laravel 11 easily.

In the world of web development, Laravel has established itself as one of the most popular frameworks due to its robustness and ease of use. The release of Laravel 11 brings enhancements and new features, and one of the most relevant aspects for developers is the implementation of shopping cart functionality. Below are the steps to easily add products to the cart in Laravel 11.

Initial Project Setup

Before you start adding products to the cart, it is essential to have a basic installation of Laravel 11. If you haven’t done so yet, you can create a new project by running the following command in your terminal:

composer create-project laravel/laravel project-name

Make sure to configure your database in the .env file.

Creating the Product Model and Controller

To manage products in your cart, you need to define a model. Run the following command to create the Product model:

php artisan make:model Product -m

This will create the model along with a migration. Next, edit the migration to include necessary fields such as name, price, and quantity. Then, run the migrations:

php artisan migrate

To manage actions in your cart, create a controller:

php artisan make:controller CartController

In this controller, you will implement the logic to add products to the cart.

Implementing Shopping Cart Functionality

To start adding products to the cart, it is advisable to implement storage of the products in the session. Within the CartController, you can create a method named add:

public function add(Request $request, $id)
{
    $product = Product::find($id);

    $cart = session()->get('cart', []);

    if(isset($cart[$id])) {
        $cart[$id]['quantity']++;
    } else {
        $cart[$id] = [
            "name" => $product->name,
            "quantity" => 1,
            "price" => $product->price,
            "photo" => $product->photo
        ];
    }

    session()->put('cart', $cart);

    return redirect()->back()->with('success', 'Product added to cart!');
}

This code snippet allows you to add a product to the cart, increasing the quantity if it already exists. It's also important to handle session storage, which is key to maintaining cart data while the user navigates the site.

Viewing the Cart

To allow users to view their shopping cart, create a method in the controller that lists all products added so far:

public function index()
{
    return view('cart.index', [
        'cart' => session()->get('cart')
    ]);
}

Remember to create the corresponding view in resources/views/cart/index.blade.php, where the products in the cart, along with their quantities and prices, will be displayed.

Conclusion

With these steps, you have successfully set up the functionality to add products to the cart in Laravel 11. This will not only enhance the user experience in your application but also facilitate the purchasing process.

If you want to delve deeper into the implementation of this and other aspects of Laravel, I invite you to read more informative articles on my blog. Don’t miss it!

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Categories

Page loaded in 24.50 ms