Hi folks ,
I have a livewire omponent where display all of my shopping carts. I have 2 issues ,
once I execute destroy all cart the livewire bladeview is giving me object not fount error (destroy method is working ) inside the balde I already check if carts count > 0 then I foreach the carts but I still get the cart->price object not found
below is the blade view
@if ( count($this->carts) > 0 )
<table class="table table-striped">
<thead>
<tr>
<th scope="col">{{__('Product Name')}}</th>
<th scope="col">{{__('Stock')}}</th>
<th scope="col" class="text-center">{{__('Quantity')}}</th>
<th scope="col" class="text-right">{{__('Single Price')}}</th>
<th scope="col" class="text-right">{{__('Total Price')}}</th> </th>
<th> </th>
<th> </th>
</tr>
</thead>
<tbody>
@forelse ($this->carts as $cart )
<tr wire:key="{{ $loop->index }}">
<td>{{ $cart->name ?? '' }}</td>
<td> </td>
<form action="{{route('update_cart')}}" method="POST">
@csrf
<input type="hidden" name="id" value="{{$cart->id ?? ''}}">
<td>
<input class="form-control" type="number" name="qty" value="{{ $cart->qty ?? ''}}" min=1 max='' required >
</td>
<td class="text-right"> $ {{ $cart->price ?? '' }} </td>
<td class="text-right"> $ {{ $cart->price ?? '' * $cart->qty ?? '' }} </td>
<td class="text-right">
<a class="btn btn-sm btn-danger" style="color:white" > X </a>
</td>
<td class="text-right" ><button type="submit" class="btn btn-success" >{{__('Update Cart')}}</button></td>
</form>
</tr>
@empty
@endforelse
<tr>
<td></td>
<td></td>
<td></td>
<td>
<button wire:click.prevent="destroy" class="btn btn-danger" >{{__('Empty Cart')}} </button>
</td>
<td><strong>{{__('Total')}} </strong></td>
<td class="text-right"><strong> ¥ {{$this->carts_subtotal}} </strong></td>
<td class="text-right"><a data-turbolinks="false" href="{{route('cashier')}}" class="btn btn-success">{{__('Check Out')}}</a></td>
</tr>
</tbody>
</table>
@else
<h3>{{__('No Carts')}}</h3>
@endif
destroy method where I perform destroy carts
public function destroy()
{
Cart::destroy();
session()->flash('added-destroy_cart-cart', 'Remove all cart items');
$refresh;
}
how I reder the component and mount
public $carts ;
public $carts_total ;
public $carts_subtotal ;
public function mount(Cart $carts,Cart $carts_total,Cart $carts_subtotal)
{
$this->carts = Cart::content();
$this->carts_total = Cart::total();
$this->carts_subtotal = Cart::subtotal();
}
public function render()
{
return view('livewire.cart-display',[
'carts' => $this->carts ,
'carts_total' => $this->carts_total ,
'carts_subtotal' => $this->carts_subtotal ,
]);
}
question is I destroy all carts inside destroy method ,after that $carts == 0 inside the blade view. and inside destroy method I call refresh the component .
//why inside below if statement still execute
@if ( count($this->carts) > 0 ){
@forelse ($this->carts as $cart )
<tr>
<td>{{ $cart->name ?? '' }}</td>
<td> </td>
<form action="{{route('update_cart')}}" method="POST">
@csrf
<input type="hidden" name="id" value="{{$cart->id ?? ''}}">
<td>
<input class="form-control" type="number" name="qty" value="{{ $cart->qty ?? ''}}" min=1 max='' required >
</td>
<td class="text-right"> $ {{ $cart->price ?? '' }} </td>
<td class="text-right"> $ {{ $cart->price ?? '' * $cart->qty ?? '' }} </td>
<td class="text-right">
<a class="btn btn-sm btn-danger" style="color:white" > X </a>
</td>
<td class="text-right" ><button type="submit" class="btn btn-success" >{{__('Update Cart')}}</button></td>
</form>
</tr>
@empty
}
//got error in those $cart->whatever object not found its laravel error