Search code examples
laravellaravel-livewire

Livewire view not updatding on record create


I cannot solve this for the life of me.

I have a list in my blade file that looks like:

@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

However, when the new $interaction gets added, there's not DOM update, until the second $interaction gets updated. (Updates with the first 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'] = "";

    }
}

What can I be doing wrong?


Solution

  • Ok, just in case anyone finds this question (as I didn't find many resources online).

    The problem was caused because the record was being inserted through a relationship, hence Livewire had no idea anything had changed.

    Doing a model->refresh() solved the problem...