So I have a list of wishlist items where each item has it's own unique product id. I output it in my view like below:
wishlist.blade.php:
<table class="table">
<!-- Starting Item Template -->
@if(count($favourite))
@foreach ($favourite as $item)
<tr>
<td class="align-top" style="max-width: 400px; border-top:white;">
<div class="row mb-5">
<div class="col-2 my-auto">
<a
href="/shop/product/{{ $item->product->parentProduct->name_slug}}?panel={{$item->product->panel_account_id}}">
<img class="responsive-img p-1"
style="height:200px;width:200px; max-width:300px;"
src="{{ asset('storage/' . $item->product->parentProduct->images[0]->path . $item->product->parentProduct->images[0]->filename)}}"
alt="Product Image">
</a>
</div>
<div class="col-6 my-auto padding-left-md">
<a style="color:black; font-weight:bold;"
href="/shop/product/{{ $item->product->parentProduct->name_slug}}?panel={{$item->product->panel_account_id}}">
<h4>{{ $item->product->parentProduct->name}}</h4>
</a>
<p class="text-capitalize">Sold by: {{$item->product->panel->company_name}}
</p>
<p class="text-capitalize">Unit Price:
<?php echo 'RM ' . number_format(($item->product->price / 100), 2); ?>
</p>
<p style="margin-top: 100px;">Added on: {{$item->created_at}}</p>
</div>
<div class="offset-2 mt-4">
{{-- Quantity: xx --}}
</div>
<div class="col-2 col-md-2 padding-button">
<button class="btn btn-md bjsh-btn-product-page font-weight-bold w-100 bjsh-button-mobile"><a
style="color:black; text-decoration:none;"
href="/shop/product/{{ $item->product->parentProduct->name_slug}}?panel={{$item->product->panel_account_id}}">
Buy Now</a>
</button>
<form id="add-to-cart-form" style="display: inline;" method="POST" action="{{ route('shop.cart.add-item') }}">
@method('POST')
@csrf
<input type="hidden" name="product_id" value="{{ $item->product_id }}">
<input type="hidden" id="product_attribute_color" name="product_attribute_color" value="">
<input type="hidden" id="product_attribute_size" name="product_attribute_size" value="">
<input type="hidden" id="product_attribute_temperature" name="product_attribute_temperature" value="">
<input type="hidden" name="productQuantity" value="1">
<button type="submit" class="btn btn-md bjsh-btn-product-page font-weight-bold w-100 bjsh-button-mobile mt-4" style="color:black; text-decoration:none;">
Add to Cart
</button>
</form>
<div class="row">
<div class="offset-2 col-5">
<a href="{{route('shop.perfect-list.destroy',[$item->product->parentProduct->id])}}" onclick="event.preventDefault(); document.getElementById('submit-form').submit();"> <i class="fa fa-trash-o fa-2x text-muted mt-4 padding-left-icon"></i></a>
<form id="submit-form" action="{{ route('shop.perfect-list.destroy',[$item->product->parentProduct->name_slug]) }}" method="POST" class="hidden">
@csrf
@method('PUT')
</form>
</div>
</div>
</div>
</div>
<hr>
</td>
</tr>
@endforeach
<!-- Ending Item Template -->
@else
<div class="row">
<div class="col-12">
<strong class="mr-2 " style="font-size:15pt;"> Your Perfect List is empty.</strong>
<a class="btn bjsh-btn-gradient" href="/shop">Continue Shopping</a>
</div>
</div>
@endif
</table>
It display the items correctly but I wanted to add a delete button to remove the item from wish list so I added a destroy method and I tried to output dd to see if I could get the product id before deleting it. Somehow, everywhere delete icon keep returning the same id.
My controller:
//Remove the specified item from storage.
public function destroy($product_id){
$userID = Auth::user()->id;
$perfectList = new Favorite;
// $perfectList = $perfectList->where('user_id',$userID)->where('product_id',$product_id)->take(1);
dd($product_id);
}
Basically the problem comes from
<a href="{{route('shop.perfect-list.destroy',[$item->product->parentProduct->id])}}"
Though, my relationship is correct between models.
Favourite model:
/**
* Get product.
*/
public function product()
{
return $this->belongsTo('App\Models\Products\Product', 'product_id');
}
Product model:
public function parentProduct()
{
return $this->belongsTo('App\Models\Globals\Products\Product', 'global_product_id');
}
Clicking the delete icon of each item just keeps returning the id for the first item (first row).
I attached an image for visual:
https://ibb.co/wSf5gXn
What did I do wrong?