【问题标题】:Livewire view not updatding on record createLivewire 视图未在记录创建时更新
【发布时间】:2021-05-27 09:04:38
【问题描述】:

我这辈子都解决不了这个问题。

我的刀片文件中有一个列表,如下所示:

@foreach($interactions as $interaction)
                <div class="border rounded px-2 my-2 op-gray w-100">
                    <div class="d-flex flex-justify-between">
                        <div>
                            <b class="fg-steel">{{$interaction->user->name ?? 'Usuario No Existente'}}</b>
                            <small class="">{{$interaction->created_at}}</small>
                        </div>
                        <span class="fg-steel">{{$interaction->InteractionType->type_name}} <small class="fg-green p-1 rounded">{{$interaction->InteractionType->status_name}}</small></span>
                    </div>
                    <div>
                        {{$interaction->body}}
                    </div>
                </div>
@endforeach

但是,当新的 $interaction 被添加时,没有 DOM 更新,直到第二个 $interaction 被更新。 (随着第一次交互而更新)。

<?php

namespace App\Http\Livewire\Client\Details;

use App\Models\Interaction;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;

class NewInteraction extends Component
{
    public $grouped_interactions;
    public $client;

    public $interaction = [];
//    public $interactions;
//    public $pending_item = [];
    public $pending_items = [];
//    public $appointment = [];
    public $appointments = [];
    public $contacts = [];

    protected $rules = [
        'interaction.type'          =>  ''
    ];

    public function render()
    {
        $interactions = $this->client->interactions;
        return view('livewire.client.details.new-interaction', compact('interactions'));
    }

    public function createNewInteraction() {

        Interaction::create([
            'interaction_type_id'       =>  $this->interaction['type'],
            'body'                      =>  $this->interaction['body'],
            'client_id'                 =>  $this->client->id,
            'user_id'                   =>  Auth::user()->id,
                            ]);

        $this->interaction['type'] = "";
        $this->interaction['body'] = "";

    }
}

我做错了什么?

【问题讨论】:

    标签: laravel laravel-livewire


    【解决方案1】:

    好的,以防万一有人发现这个问题(因为我没有在网上找到很多资源)。

    问题是因为记录是通过关系插入的,因此 Livewire 不知道有什么变化。

    做一个模型->refresh() 解决了问题...

    【讨论】:

    • 如果您只想刷新一个/某些属性,也可以使用fresh()
    猜你喜欢
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 2014-04-24
    相关资源
    最近更新 更多