Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

joffreypersia's avatar

Need help to understand why sometimes the data of my table is lost after a function and I need to force query again the posts

Hi,

I have a parent LV component where I would like to display a table of Users of my app : (it is flux component but nothing special about them)

The view

<div>
    <flux:table>
        <flux:columns>
            <flux:column>ID</flux:column>
            <flux:column>Nom</flux:column>
            <flux:column>Email</flux:column>
            <flux:column>Rôle</flux:column>
            <flux:column>Date de création</flux:column>
            <flux:column>
                <span class="sr-only">Actions</span>
            </flux:column>
        </flux:columns>

        <flux:rows>
            @foreach($users as $user)
                <livewire:user-row :user="$user" :key="$user->id" />
            @endforeach
        </flux:rows>
    </flux:table>
    ...
</div>

The UserRow.php

And the view :

I tried to do exactly what Caleb Porzio did in its tutorial but for him the delete keeps the previous data, but not for me

I have different usecases of using foreach of child livewire component and sometimes the data is kept, sometimes not. I don't understand why.

Here is Caleb screenshot of its delete function on the parent livewire component

https://share.cleanshot.com/Q1jDbX9t

Thank you for any insights :)

0 likes
2 replies
LaryAI's avatar
Level 58

The issue you're experiencing is related to how Livewire handles data binding and component reactivity. When you delete a user, the $users property in your parent component (Show) is not automatically updated unless you explicitly refresh it, which is why your table appears empty after a delete operation if you don't re-query the users.

Here's a breakdown of why this happens and how you can address it:

Why the Data is Lost

  1. Data Binding: In Livewire, properties are not automatically reactive in the sense that they don't automatically update when the underlying data changes in the database. When you delete a user, the $users property still holds the old data unless you refresh it.

  2. Component Lifecycle: The mount method is only called once when the component is initialized. It doesn't get called again unless the component is re-rendered from scratch, which doesn't happen in your case when you delete a user.

Solution

To ensure that your table reflects the current state of the database after a user is deleted, you need to refresh the $users property. You are already doing this by re-querying the users in the deleteUser method:

public function deleteUser($id)
{
    $user = User::findOrFail($id);
    $user->delete();
    $this->users = User::all(); // Refresh the users list

    Flux::modal('delete-user')->close();
    $this->dispatch('alert', ['title' => 'L\'utilisateur a bien été supprimé', 'type' => 'success']);
}

This line is necessary to update the $users property with the current list of users from the database. Without it, the $users property would still contain the deleted user, leading to an inconsistency between your database and the displayed data.

Alternative Approach

If you want to avoid re-querying the database, you could manually remove the user from the $users array after deletion:

public function deleteUser($id)
{
    $user = User::findOrFail($id);
    $user->delete();

    // Remove the user from the local $users array
    $this->users = $this->users->filter(function ($user) use ($id) {
        return $user->id !== $id;
    });

    Flux::modal('delete-user')->close();
    $this->dispatch('alert', ['title' => 'L\'utilisateur a bien été supprimé', 'type' => 'success']);
}

This approach avoids the need to re-query the database, but it requires that the $users property is a collection or array that you can manipulate.

Conclusion

The key takeaway is that Livewire doesn't automatically sync your component's properties with the database. You need to manage this synchronization manually, either by re-querying the database or by updating the properties directly in your component logic.

Chingy's avatar
Chingy
Best Answer
Level 6
  1. Make your public $users a computed property. Livewire can't handle Collections well
  2. It makes sense that if you remove the line where you do $this->users = User::all(); after delete because the $users is run once in mount and that way you are refreshing. A newbie would add it on render which would re-query everytime it (re-)renders. I strongly suggest you make properties that are not getting manipulated, computed properties (such as users). Livewire better handle for such advanced property types, and it makes 1 query per http lifecycle

Please or to participate in this conversation.