删除laravel中的购物车条件

时间:2018-02-13 04:06:20

标签: php laravel

我尝试从购物车中的conditions移除items,我获得null个值。

CODES

controller

public function removecondition(Request $request, $id) {
        $id = $request->input('proId');
        $conditionName = $request->input('conditionname');
        dd($conditionName, $id);
        // Cart::removeItemCondition($id, $conditionName);
        // Session::flash('success', 'Product condition successfully removed from your cart.');
        // return redirect()->back();
    }

route

Route::get('removecondition/{id}', 'CartController@removecondition')->name('removecondition');

blade

// getting name of condition
<input type="hidden" value="{{$condition->getName()}}" name="conditionname">

// getting item id
<input type="hidden" name="proId" value="{{$item->id}}">

//delete button
<a class="pull-left text-danger" href="{{route('removecondition', $item->id)}}"><i class="fa fa-times"></i></a>

我的包裹文件

删除特定项条件:Cart :: removeItemCondition($ itemId,$ conditionName)

/**
* remove a condition that has been applied on an item that is already on the cart
*
* @param $itemId
* @param $conditionName
* @return bool
*/
Cart::removeItemCondition($itemId, $conditionName)

source

更新

我刀片的轻型版本:

@foreach($items as $item)
  {{$item->id}}

  @foreach($item->conditions as $condition)
     {{$condition->getName()}}

     <input type="hidden" value="{{$condition->getName()}}" name="conditionname"> 

     <a class="pull-left text-danger" href="{{route('removecondition', $item->id)}}"><i class="fa fa-times"></i></a>
  @endforeach


  <input type="hidden" name="proId" value="{{$item->id}}">
@endforeach

1 个答案:

答案 0 :(得分:1)

您无法使用$request->input('field_name');检索该值,因为您没有使用表单提交请求。我认为现在代码的最佳方法就是这样。

首先,更改您的刀片代码&lt; a&gt;标记为

<a class="pull-left text-danger" href="{{route('removecondition', ['id' => $item->id, 'condition' => $condition->getName()])}}"><i class="fa fa-times"></i></a>

因此它会在网址中传递商品ID和条件,然后将您的路线更改为

Route::get('removecondition/{id}/{condition}', 'CartController@removecondition')->name('removecondition');

最后,您现在可以像这样访问函数中的值

public function removecondition($id, $condition) {
    dd($id, $condition);
}

希望它有所帮助!