对成员函数 has() 的 laravel 购物车调用在 null (0) 上



当我使用Darryldecode\Cart时会出现此问题 edit(( 方法 公共函数编辑($id( {

$product=Product::find($id);
//  Cart::add(455, 'Sample Item', 100, 2, array());

Cart::add($id,$product->name,1,$product->price,['type'=>'PDF']);
return back();

和购物车/索引刀片.php

<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>qty</th>
<th>Type</th>
</tr>
</thead>
<tbody>
@foreach($cartItems as $cartItem)
<tr>
<td>{{$cartItem->name}}</td>
<td>{{$cartItem->price}}</td>
<td>{{$cartItem->qty}}</td>
<td>{{$cartItem->options->has('type')?$cartItem->options->type:''}}</td>
</tr>
@endforeach
</tbody>
</table>

所以我有这个问题,想解决它

ErrorException (E_ERROR( 对成员函数 has(( 的调用 on null

它需要验证 我该怎么办?

您可以使用optional助手:

optional($cartItem->options)->has('type') ? $cartItem->options->type : ''

最新更新