【发布时间】:2023-01-27 03:33:29
【问题描述】:
我有一系列产品,我试图将单个项目作为快速查看的参数传递,但我不能。有人有任何解决方案吗?提前致谢。
@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>
在我的控制器中,我正在尝试做这样的事情:
public function quickView($product)
{
$this->view_product = $product;
}
我试图传递对象,但出现以下错误:
htmlspecialchars() expects parameter 1 to be string, object given
【问题讨论】:
-
你能分享你是如何声明“view_product”的吗?您是否使用公共字符串 $view_product?如果是,将其更改为 public $view_product 并重试
-
我是这样做的: public $view_product;
-
我认为您的错误源于 ... wire:click="quickView({{ $product }})" ... 为什么您需要在 livewire 刀片组件中执行 "$product = (object) $product" ?你不能从你的组件中返回模型产品列表吗?
-
我不需要列表,我需要列表中的特定项目才能在快速查看模式中显示
-
所以如果它是一个项目,你为什么要把它转换成一个对象?它是一组对象吗?