Search code examples
phplaravelcart

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' => ['CartController@add', $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 href="/inventory/view-product/{{ $product->id }}">{{ $product->id }}</a></td>
    <td><a 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


Solution

  • 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);