dongtaogou6226 2019-03-11 07:00
浏览 36
已采纳

重定向到视图时Gloudemans Cart内容为空,但使用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

  • 写回答

1条回答 默认 最新

  • douhong1703 2019-03-11 09:29
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题