1

$cartItems contains all the rows of products from database and I am using this inside a blade file.

I want to pass this $cartItems back to a controller from this blade file

Note: $cartItems is from index() function in a controller like below.

$cartItems = DB::table('products')->whereIn('id', $cartItemsArray)->get();
return view('cart.index', compact('cartItems')

Below is my code.

index.blade.php

<a href="{{route('cart.checkout',$cartItems)}}" class="site-btn">Proceed to checkout</a>

web.php

Route::get('/cart/checkout/{cartItems}', 'CartController@checkout')->name('cart.checkout')->middleware('auth');

CartController.php

public function checkout($cartItems)
{
   dd($cartItems);
   return view('cart.checkout');
}

The error I am getting is,

Missing required parameters for [Route: cart.checkout] [URI: cart/checkout/{cartItems}]. (View: E:\github\LARAVEL\Deal-Ocean\resources\views\cart\index.blade.php)

chanafdo
  • 5,016
  • 3
  • 29
  • 46
Backstreet Imrul
  • 102
  • 2
  • 15

2 Answers2

3

You can use a form to send data back to server

Update your route from get to post

Route::post('/cart/checkout', 'CartController@checkout')->name('cart.checkout')->middleware('auth');

Use a form to post data to server. You can pass any additional data along with the request as well.

<form method="post" action="/cart/checkout">
@foreach($cartItems as $item)
    <input name="cartItems[]" value="{{ $item->id }}"
@endforeach
<button class="site-btn">Proceed to checkout</button>
</form>

And in your controller use Request to access data

public function checkout(Request $request)
{
    $cartItems = DB::table('products')->whereIn('id', $request->get($cartItems))->get();
   dd($cartItems);
   return view('cart.checkout');
}

If you want to proceed with the get request you should be able to do as follow

As $cartItems is a collection of products. So you can send the product ids and query the products using the ids from request.

<a href="{{ route('cart.checkout', ['cartItems' => $cartItems->pluck('id')->toArray()]) }}" 
    class="site-btn">Proceed to checkout</a>

Update controller

public function checkout(Request $request)
{
    $cartItems = DB::table('products')->whereIn('id', $request->get($cartItems))->get();
   dd($cartItems);
   return view('cart.checkout');
}
chanafdo
  • 5,016
  • 3
  • 29
  • 46
0

Why use the same code logic of the index() method in the checkout method in the CartController.

the checkout method will look like this:

   $cartItems = DB::table('products')->whereIn('id', $cartItemsArray)->get();
   return view('cart.checkout', compact('cartItems');
  • I have cart page in index.blade. I have products with number of occurance in that page. and calculated the total price. Now I had to send the `$cartItems` and `$totalPrice` to `CartController`. and use it for further calculation. But if its not possible to send the data directly. then I have to query again in `checkout method`. I can send normal `variable` but cant send the array – Backstreet Imrul Jul 23 '20 at 12:52