I have an array of product and i am trying to pass a single item as a parameter for quick view, but I can’t. anyone have any sollution? thanks in advance.
@foreach ($products as $product)
@php
$product = (object) $product;
@endphp
<div class="col-md-3 mb-4">
<div class="product-item product-item-border custom-product-item">
<a class="product-item-thumb" href="shop-single-product.html">
@if (count($product->related_images) > 0)
<img src="{{ $product->related_images[0]['image'] }}" width="233" height="245" alt="Image-HasTech">
@endif
</a>
<div class="product-item-action">
<button type="button" class="product-action-btn action-btn-wishlist" data-bs-toggle="modal" data-bs-target="#action-WishlistModal">
<i class="icon-heart"></i>
</button>
<button type="button" class="product-action-btn action-btn-compare" data-bs-toggle="modal" data-bs-target="#action-CompareModal">
<i class="icon-shuffle"></i>
</button>
<button type="button" wire:click="quickView({{ $product }})" class="product-action-btn action-btn-quick-view">
<i class="icon-magnifier"></i>
</button>
</div>
<div class="product-bottom">
<div class="product-item-info text-center pb-6">
<h5 class="product-item-title mb-2"><a href="shop-single-product.html">{{ $product->product_name }}</a></h5>
{{-- <div class="product-item-price mb-0">{{ $product->default_price }}<span class="price-old">{{ $product->default_price }}</span></div> --}}
</div>
<div class="d-flex justify-content-between">
<div class="ms-4 product-item-price mb-4">{{ $product->default_price }}</div>
<button type="button" wire:click="addToCart({!! $product->id !!})" class="info-btn-cart me-4 mb-4"><i class="icon-handbag"></i></button>
</div>
</div>
</div>
</div>
@endforeach
<div class="col-12">
<div class="text-center">
<div wire:click="nextPage" type="button" class="btn btn-primary">Load more</div>
</div>
</div>
and in my controller I am trying to do something like this:
public function quickView($product)
{
$this->view_product = $product;
}
I tried to pass the object but I am getting error such:
htmlspecialchars() expects parameter 1 to be string, object given
2
Answers
The error happens because in blade
{{ }}
will automatically escape the output. And this function only escapes strings.Instead what you can do is
And within your livewire component
EDIT:
If you do not want to add "extra query" inside quickView()
I suggest you change how $products collection is collected
This will resulting array list of object product.
After that within quickView() do the following
Extra note: please do add some kind of check or validation in case product with selected id is not found
It may be a little too late but I got to solve something similar by type hinting the object class. From the docs
So maybe you can do something like this: