Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Head0nf1re's avatar

Trying to pass data to store method

I'm following a tutorial and trying to implement this cart plugin.

I have a resource ProductController and CartController. In the view product.list I list all my products, in there when adding to cart I'm trying to pass the $product to the CartController (cart.store). When I ddd($product) in the Cart store method it does not contain nothing related to the product.

I could simply make input for every attribute I want to pass, but I'm trying to do this way...

What I'm I doing wrong?

CartController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Gloudemans\Shoppingcart\Facades\Cart;

use App\Models\Product;

class CartController extends Controller
{

    public function index()
    {
        return view('cart.list');
    }


// I had Request $request but replaced to Product $product like in the tutorial

    public function store(Product $product)
    {
        $duplicates = Cart::search(function ($cartItem, $rowId) use ($product) {
            return $cartItem->id === $product->id;
        });

        ddd($product);

        if ($duplicates->isNotEmpty()) {
            return redirect()->route('cart.index')->with('success', 'Item is already in your cart!');
        }

        Cart::add($product->id, $product->tite, 1, $product->price)->associate('App\Models\Product');

        ddd('teste');

        return redirect()->route('cart.index')->with('success', 'Item was added to your cart!');
    }

product (list.blade.php - from ProductController resource ):

@foreach ($products as $product)
                <li class="list-group-item">
                    <!-- Custom content-->
                    <div class="media align-items-lg-center flex-column flex-lg-row p-3">
                        <div class="media-body order-2 order-lg-1">
                            <h5 class="mt-0 font-weight-bold mb-2">{{ $product->title }}</h5>
                            <p class="mt-0 text-muted font-weight-bold mb-2">category: {{ $product->category->name }}</p>
                            <p class="font-italic text-muted mb-0 small">{{ $product->description }}</p>
                            <div class="d-flex align-items-center justify-content-between mt-1">
                                <h6 class="font-weight-bold my-2">{{ $product->price }} €</h6>
                            </div>

                            <form action="{{ route('cart.store', $product) }}" method="POST">
                                @csrf
                                <button type="submit" class="btn btn-dark">Add Cart</button>
                            </form>

                        </div>
                        <img src="{{ asset('uploads/products/'.$product->image) }}" alt="" width="200" class="ml-lg-5 order-1 order-lg-2 rounded">
                    </div> <!-- End -->
                </li> <!-- End -->

                </br></br>
                @endforeach

Debug

This is what I get when I ddd($product);in CartController store method:

App\Models\Product {#1270 ▼
  #fillable: array:6 [▼
    0 => "title"
    1 => "category_id"
    2 => "code"
    3 => "description"
    4 => "price"
    5 => "image"
  ]
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▼
    0 => "*"
  ]
}

Route file (web.php)

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\HomeController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\CartController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/


Route::view('/', 'welcome');

Auth::routes();

Route::get('/home', [HomeController::class, 'index'])->name('home');

Route::group(['middleware' => ['auth']], function() {

    Route::resource('users', UserController::class)->except(['show']);
    Route::resource('products', ProductController::class)->except(['show']);
    Route::resource('cart', CartController::class)->except(['show']);
});


Route::group(['middleware' => ['role:admin']], function () {

    Route::resource('categories', CategoryController::class)->except(['show']);
});
0 likes
4 replies
Head0nf1re's avatar

@silencebringer Added the route file to question now, it's after the debug part. Sorry, I didn't undertand the second part?

Does url contains product param for store? Because default one store url doesn't

Edit: Ohhh, I think I understand what you mean. No, I didn't make any custom route for cart.store in route files How could I add it, like this?

Route::resource('cart', CartController::class)->parameters([
    'cart.store' => 'products'
]);

Edit 2: Tried this and did't work. Also tried this in route and the product info didn't show up in ddd($product):

Route::post('/cart/{product}', 'CartController@store');
Route::resource('cart', CartController::class)->except(['show']);

SilenceBringer's avatar
Level 55

@head0nf1re type php artisan route:list in terminal and see

Your cart.store url is post request to '/cart' it do not accept any params. You need to define it by yourself

Route::post('cart/{product}', [CartController::class, 'store'])->name('cart.store');
Route::resource('cart', CartController::class)->except(['show', 'store']);
1 like
Head0nf1re's avatar

Thanks for the help, that works. I was close, why didn't this work, It's old syntax?

Route::post('/cart/{product}', 'CartController@store');

Didn't find clear info in Laravel docs

Please or to participate in this conversation.