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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!