Get product information of items added in cart

Stefan Bogdanescu
Founder & Senior Architect · 14 August 2026

Accessing Product Information in the Shopping Cart

In an e-commerce application built on Laravel, accessing product information from the shopping cart involves understanding how products are added to the cart and how that data is stored. In this blog post, we’ll walk through the process of retrieving essential product details like ID, price, and category when a user adds items to their cart.

Understanding the Shopping Cart Structure

In Laravel, there isn’t a built-in shopping cart model, but you can create one using the HasMany relationship with the App\Models\Product model. Here’s an example of how you might structure your Cart model:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Cart extends Model
{
    use HasFactory;

    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class);
    }
}

Adding Products to the Cart

When a user adds a product to their cart, you typically handle this via an AJAX request or a form submission. In your controller action (e.g., CartController@add), you should create a new Cart instance and associate it with the selected product.

Here’s how you might do that:

public function add(Request $request)
{
    // Validate the incoming data...

    $cart = Cart::create([
        'user_id' => auth()->id(),
        'product_id' => $request->input('product_id'),
        'quantity' => $request->input('quantity', 1),
    ]);

    return response()->json($cart);
}

Retrieving Product Information from the Cart

Now, let’s look at how to retrieve product information when a user views their cart. You can achieve this by eager loading the product relationship in your CartController.

public function index()
{
    $carts = Cart::where('user_id', auth()->id())
        ->with('product')
        ->get();

    return view('cart.index', compact('carts'));
}

In your Blade template (resources/views/cart/index.blade.php), you can now access the product details for each item in the cart:

@foreach ($carts as $cart)
    <div class="cart-item">
        <h2>{{ $cart->product->name }}</h2>
        <p>Price: ${{ $cart->product->price }}</p>
        <p>Category ID: {{ $cart->product->category_id }}</p>
        <!-- Display other product attributes here -->
    </div>
@endforeach

Conclusion

Accessing product information from the shopping cart in a Laravel application is straightforward once you have established the proper relationships and data storage. By using eager loading, you can efficiently retrieve all necessary product details when displaying the user’s cart.

Remember that this is just one approach to managing a shopping cart in Laravel. The specific implementation might vary depending on your application’s requirements and the e-commerce package or library you use (if any).

If you’re looking for more advanced features like handling multiple user sessions, coupon codes, or shipping calculations, consider exploring dedicated e-commerce solutions built on top of Laravel, such as Laravel Company’s Store or the Laravel Cashier package.

Happy coding!

Our network