Laravel 使用livewire让我的modal拥有正确的产品

Laravel 使用livewire让我的modal拥有正确的产品,laravel,laravel-8,Laravel,Laravel 8,我正在学习使用livewire和laravel。我遇到的问题是,当我点击我的删除按钮时 我弹出的模式不是获取我选择的产品,而是获取表中的最后一个产品 这是我的Products.php <?php namespace Modules\Products\Http\Livewire; use Modules\Products\Models\Product; use Livewire\Component; class Products extends Component { publ

我正在学习使用livewire和laravel。我遇到的问题是,当我点击我的删除按钮时 我弹出的模式不是获取我选择的产品,而是获取表中的最后一个产品

这是我的Products.php

<?php

namespace Modules\Products\Http\Livewire;

use Modules\Products\Models\Product;
use Livewire\Component;

class Products extends Component
{
    public $modal = false;

    protected $listeners = [
        'productDeleted' => 'deleteModal',
        'close' => 'close',
        'confirmDelete' => 'delete'
    ];

    public function render()
    {
        return view('products::livewire.products', [
            'products' => Product::all()
        ]);
    }

    public function deleteModal($id)
    {
        $this->modal = true;
    }

    public function close()
    {
        $this->modal = false;
    }
}

对我来说,我使用了事件,它工作得非常完美

//component
protected $listeners = ['ProductId'];
    public $productId;

    public function ProductId($id)
    {
        $this->productId= $id;
        $this->dispatchBrowserEvent('ProductId');
    }
//刀片
显示模态
//你可以把$key放在你想要的地方
{{$key}}
...
接近
保存更改

window.addEventListener('ProductId',event=>{
$(“#示例模态”).model('show');
})

我希望它能对你的工作有所帮助:)

deletemodel($id)
这里
$id
你没有得到?@KamleshPaul-即使我找到了产品,我不知道如何将其传递给模式,以便我可以删除正确的产品当您有组件和事件时,为什么需要传入模型我正在使用模式确认用户想要删除产品,但我遇到的问题是,它没有获得正确的产品来删除make new property
public$isConfrm=false并基于onClick中的显示模型
{{ $product->title}}
//component
protected $listeners = ['ProductId'];
    public $productId;

    public function ProductId($id)
    {
        $this->productId= $id;
        $this->dispatchBrowserEvent('ProductId');
    }
//blade

<button type="button" wire:click="$emit('ProductId',{{ $product->id}})" >show modal </button>

// you can put $key where you want
<!-- Modal -->
    <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">{{ $key }}</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    ...
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>
<script>
window.addEventListener('ProductId', event => {
    $("#exampleModal").modal('show');
  })
</script>