Reputation: 1738
Can't figure how to pass the selected item->id to my route
I'm generating a dropdown from database.
<div class="form-group">
<label for="">Tallas disponibles</label>
<select class="form-control" name="" id="">
@foreach ($subproducts as $subproduct)
<option value="{{ $subproduct->id}}">{{$subproduct->description}}</option>
@endforeach
</select>
</div>
Using a <form action="{{ route ('cart.add', $subproduct->id)}}"
doesn't work also adding a <a href="{{ route ('cart.add', $subproduct->id)}}" >
after dropdown wont also do the job.
Route is
Route::get('/add-to-cart/{product}', 'CartController@add')->name('cart.add');
and controller
public function add (Product $product) {
// dd ($product);
\Cart::add(array(
'id' => $product->id,
'name' => $product->description,
'price' => $product->price,
'quantity' => 1,
'attributes' => array(),
'associatedModel' => $product
));
return back();
}
It's probably something easy to figure, but I can't see what it is.
Thank you all,
Upvotes: 0
Views: 242
Reputation: 177
<form action="{{ url('cart.add') }}" method="get">
<div class="form-group">
<label for="">Tallas disponibles</label>
<select class="form-control" name="product" id="">
@foreach ($subproducts as $subproduct)
<option value="{{ $subproduct->id}}">{{$subproduct->description}}</option>
@endforeach
</select>
</div>
<button type="submit">submit</button>
</form>
Your route
Route::get('/add-to-cart', 'CartController@add')->name('cart.add');
Your controller
use Illuminate\Http\Request;
..
public function add (Request $request) {
dd($request->input('product')) //check
}
Upvotes: 1
Reputation: 61
Your route's parameter is {product}
but you're sending the ID as $subproduct
, and your syntax for generating the route is incorrect.
It should look like this:
route('cart.add', ['product' => $subproduct->id])
You can read more on URL generation here.
Upvotes: 0