Home > Software design >  Livewire error: Calling a member function getFilteredUsers() to null
Livewire error: Calling a member function getFilteredUsers() to null

Time:10-28

I have created an address table with Livewire. The admin can filter the results by Activestatus (boolean). In my mount method I initialise my UserService class.

Livewire mount Function

public funktion mount() {
    $this->userService = new UserService();
}

In my method filterUser() looks like this:

Livewire filterUser() Function

public function filter(string $key, bool $value): void
{
    $this->users = $this->userService->getFilteredUsers('is_active', $value);
}

Unfortunately, I then get this error Error: Call to a member function getFilteredUsers() on null.

Strangely, the service method works when I call it in the mount or render function.

this works - Livewire mount function

public funktion mount() {
    $this->userService = new UserService();
    dd( $this->users = $this->areaService->getFilteredUsers('is_active', true) );
}

Question:

Does anyone know why this is?

CodePudding user response:

Each Livewire component undergoes a lifecycle. Lifecycle hooks allow you to run code at any part of the component's lifecyle, or before specific properties are updated. In your case you are using the mount hook. According to the documentation this hook is only executed once when this component is mounted for the first time, but it is not called on subsequent requests.

mount: Runs once, immediately after the component is instantiated, but before render() is called

You should look for another hook that suits your needs. Please take a look at the documentation and find the right hook. My suggestion is to use the booted hook

  • Related