Gloudemans Cart Content Is Empty When Redirected To A View But Is Visible With Dd()
I want to redirect to the index view after an item is added to the cart. But Cart::content appears not set. I can return the content using dd();
Here is the form in view:
{!! Form::open(['action' => ['[email protected]', $account->id], 'method' => 'POST']) !!}
{{ Form::hidden('product_id', $product->id) }}
{{ Form::hidden('product_name', $product->product_name) }}
{{ Form::hidden('price', $product->price) }}
<tr>
<td><a target="_blank" rel="nofollow noreferrer" href="/inventory/view-product/{{ $product->id }}">{{ $product->id }}</a></td>
<td><a target="_blank" rel="nofollow noreferrer" href="/inventory/view-product/{{ $product->id }}">{{ ucwords($product->product_name) }}</a></td>
<td><strong>{{ number_format($product->price, 2) }}</strong> per {{ ucwords($product->unit) }}</td>
<td>{{ Form::number('qty', '1', ['style' => 'width:50px']) }}</td>
<td>{{ Form::button('Add', ['type' => 'submit', 'name' => 'action', 'value' => 'add', 'class' => 'btn btn-primary']) }}</td>
</tr>
{{ Form::hidden('_method', 'PUT') }}
{!! Form::close() !!}
Here are the index and add functions in the controller:
public function index($id)
{
$account = Customer::find($id);
$products = Product::all();
$business_units = BusinessUnit::all();
$cartItems = Cart::content();
//dd($account);
return view('cart.index')
->with('account', $account)
->with('products', $products)
->with('business_units', $business_units);
}
public function add(Request $request, $id)
{
//dd($request);
$product_id = $request->input('product_id');
$product_name = $request->input('product_name');
$price = $request->input('price');
$qty = $request->input('qty');
$url = $request->input('url');
Cart::add($product_id, $product_name, $qty, $price);
$cartItems = Cart::content();
//dd($cartItems);
return redirect()->back()->with('cartItems', $cartItems);
}
I want to use a redirect because it has to go back to the index needs with the parameters for the search function.
This is how I am trying to access the contents of the Cart:
@if(isset($cartItems))
@foreach ($cartItems as $cartItem)
<tr>
<td class="center">1</td>
<td class="left strong">Jasmine Rice</td>
<td class="left">Long-grain variety of fragrant rice.</td>
<td class="right">99.99</td>
<td class="center"><input type="number" value="1" style="width:50px"></td>
<td class="right">₱2,499.75</td>
</tr>
@endforeach
@else
<tr>
<td colspan="6" class="text-center">No item selected</td>
</tr>
@endif
It would yield "No item selected" but I again I am able to see the contents using dd();
I hope you guys can help! laravel
Answer
I think if you pass cartItems
to your view in the index()
method you should be able to see the cart items, because the updated data should be fetched again from the database (including the newly created items). There won't be a need to pass the cartItems
along with the redirect then. I don't think the with
method does anything after the back()
method is called in any case.
So try:
$cartItems = Cart::content();
return view('cart.index')
->with('account', $account)
->with('products', $products)
->with('cartItems', $cartItems)
->with('business_units', $business_units);
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?