Laravel Livewire组件保存后不刷新

发布于 2025-02-09 23:28:23 字数 1973 浏览 2 评论 0原文

我陷入了我的感觉应该很容易的事情,但这只是不起作用。我将记录保存到数据库中的集合中,该集合显示为页面上的列表。因此,保存该记录后,我想自动更新页面以显示更新的列表,包括新记录。

刀片组件非常简单地循环循环。我已经删除了一些代码以简单,但是我知道这一切都必须位于一个Div元素中:

<div>
    @foreach ($statements as $statement)
            <div class="col-12 col-md-4 mb-2" wire:key="statement-{{ $statement->id }}">
                ...other stuff.
            </div>
        @endforeach
</div>

在我目前正在执行此操作的LiveWire组件中,这是不起作用的:

class VendorStatement extends Component
{
    public Location $location;
    public Statement $statement;
    public $statements;

    protected $listeners = [
        'fileUploaded'      => 'save',
        'refreshComponent'  => '$refresh',
    ];


    public function mount(Location $location)
    {
        $this->location = $location;
        $this->refreshStatements();
    }

    public function render()
    {
        return view('livewire.locations.statements');
    }

    public function refreshStatements()
    {
        $this->statements = $this->location->statements;
        $this->render();
    }

    public function save($uploadedFile)
    {
        if ($path = Storage::disk('s3')->put(env('APP_ENV') . '/statements/' . $uploadedFile[1], file_get_contents($uploadedFile[0]))) {
            $this->statement = new Statement([
                'location_id'   => $this->location->id,
                'file_name'     => $uploadedFile[1],
                'path'          => $path,
                'uploaded_by'   => Auth::user()->id,
            ]);

            $this->statement->save();
        }

        StatementUploadedEvent::dispatch($this->statement);
        $this->refreshStatements();
    }
}

我' VE试验:

我尝试使用$ this-&gt; emit('RefReshComponent');保存后而不是调用refreshstatements方法。

我也尝试再次设置$ this-&gt;语句的值

不知道为什么它不起作用。

I'm stuck on what I feel like should be easy, but it's just not working. I'm saving a record to the database for the collection that's displayed as a list on the page. So after saving that record, I want to update the page to show the updated list, including the new record, automatically.

The blade component is pretty straight forward foreach loop over the collection. I've removed some of the code for simplicity, but I'm aware that it all must be in a single div element:

<div>
    @foreach ($statements as $statement)
            <div class="col-12 col-md-4 mb-2" wire:key="statement-{{ $statement->id }}">
                ...other stuff.
            </div>
        @endforeach
</div>

and in my Livewire component I'm doing this currently, which is not working:

class VendorStatement extends Component
{
    public Location $location;
    public Statement $statement;
    public $statements;

    protected $listeners = [
        'fileUploaded'      => 'save',
        'refreshComponent'  => '$refresh',
    ];


    public function mount(Location $location)
    {
        $this->location = $location;
        $this->refreshStatements();
    }

    public function render()
    {
        return view('livewire.locations.statements');
    }

    public function refreshStatements()
    {
        $this->statements = $this->location->statements;
        $this->render();
    }

    public function save($uploadedFile)
    {
        if ($path = Storage::disk('s3')->put(env('APP_ENV') . '/statements/' . $uploadedFile[1], file_get_contents($uploadedFile[0]))) {
            $this->statement = new Statement([
                'location_id'   => $this->location->id,
                'file_name'     => $uploadedFile[1],
                'path'          => $path,
                'uploaded_by'   => Auth::user()->id,
            ]);

            $this->statement->save();
        }

        StatementUploadedEvent::dispatch($this->statement);
        $this->refreshStatements();
    }
}

Things I've Tried:

I've tried using $this->emit('refreshComponent'); after the save instead of calling the refreshStatements method.

I've tried just setting the value of $this->statements again as well.

Not sure why it isn't working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空气里的味道 2025-02-16 23:28:23

我能够通过像这样修改我的代码来使其起作用:

public function render()
    {
        $this->refreshStatements();
        return view('livewire.locations.statements');
    }

    public function refreshStatements()
    {
        $this->statements = $this->location->statements;
    }

我仍然不明白为什么这不会按照我的配置方式工作,但至少它可以正常工作。

I was able to get this to work by modifying my code like so:

public function render()
    {
        $this->refreshStatements();
        return view('livewire.locations.statements');
    }

    public function refreshStatements()
    {
        $this->statements = $this->location->statements;
    }

I still don't understand why this wouldn't work the way I had it configured, but at least it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文