【问题标题】:Is there any way to pass object as a parameter in laravel livewire?有没有办法在 laravel livewire 中将对象作为参数传递?
【发布时间】: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" ?你不能从你的组件中返回模型产品列表吗?
  • 我不需要列表,我需要列表中的特定项目才能在快速查看模式中显示
  • 所以如果它是一个项目,你为什么要把它转换成一个对象?它是一组对象吗?

标签: php laravel


【解决方案1】:

发生错误是因为在 blade {{ }} 中会自动转义输出。而且这个函数只对字符串进行转义。

相反,你可以做的是

 wire:click="quickView( {{ $product->id }} )

在你的 livewire 组件中

public function quickView($product_id)
{
    // I assume $products is collection
    $this->view_product = $this->products->where('id', $product_id)->first();
}

编辑:
如果您不想在 quickView() 中添加“额外查询”
我建议您更改 $products 集合的收集方式

$this->products = $existing_query->get()->keyBy('id');

这将生成对象产品的数组列表。

之后在 quickView() 中执行以下操作

public function quickView($product_id)
{
    $this->view_product = $this->products[$product_id];
}

额外注意:如果找不到具有所选 ID 的产品,请添加某种检查或验证

【讨论】:

  • 这可能是一个解决方案,但我想在不查询的情况下显示数据
  • 你不再做任何 sql 查询,因为你已经有了 $this->products,它只是从现有集合中获取一个产品
  • 我已经通过添加另一个解决方案进行了编辑,您需要在其中添加 ->keyBy(id) 到 $this->products 查询中
【解决方案2】:

可能有点太晚了,但我必须通过类型提示对象类来解决类似的问题。来自docs

所以也许你可以这样做:

public function quickView(Product $product)
{
    $this->view_product = $product;
}

【讨论】:

    猜你喜欢
    • 2020-01-18
    • 1970-01-01
    • 2017-07-10
    • 2017-09-25
    • 2015-07-08
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多