modal icon indicating copy to clipboard operation
modal copied to clipboard

On closeModel(), getting error Livewire\Features\SupportLegacyModels\EloquentModelSynth::setDataOnModel(): Argument #1 ($model) must be of type Illuminate\Database\Eloquent\Model, null given

Open phila088 opened this issue 1 year ago • 1 comments

When calling $this->closeModal() I am getting the above error. This is called as it is in the documentation, and I cannot locate anything else in the issues regarding this. Not sure where to start.

I am calling function confirmDelete() from a wire:click() on a button in the modal to confirm the delete of a record. The record deletes properly, it runs $this->dispatch() fine, and closes the modal, but then throws the error once closed.

EDIT:

After further testing, it has to do with the events. Any events fired cause the error. If I run $this->dispatch() the error shows after close, but is present on mobile view. If it is fired with $this->closeModalWithEvents() it is displayed after closing the modal.

phila088 avatar Apr 13 '24 20:04 phila088

I ran into the same issue this week and thought I'd post here in case others find themselves in this situation too.

It happens because, by default, the modal component is not destroyed when $this->closeModal() is called. Instead, the state is kept alive. Because the record is deleted, it then throws the error as the model doesn't exist any more but it's properties are still being referenced. You need to override the destroyOnClose() method in your component class. It is referenced in the README, under the Changing Modal Properties section.

You override the static method like this:

public static function destroyOnClose(): bool
{
    return true;
}

You can also configure this globally by publishing the configuration and changing the default behaviour.

andrew1601 avatar Aug 13 '24 20:08 andrew1601