Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easy way of keeping model instance alive? #143

Closed
faustbrian opened this issue Aug 7, 2019 · 3 comments

Comments

@faustbrian
Copy link
Contributor

commented Aug 7, 2019

102f087

Recently a breaking changed was introduced for when the updating and updated events are called which broke an application I am working on. I used the way those lifecycle events were called to keep the models in my component updated to be instances instead of arrays.

What would be the recommended way now to keep a model instance inside the component after the change to those events? It's rather tedious to have multiple manual Model::findOrFail($this->model['id']) calls inside a component so the previous method with lifecycle events was an easy and cheap (with model caching) way of keeping the model instance alive.

This code no longer works because updating is never called in time.

<?php

namespace App\Http\Livewire\Concerns;

use App\Models\Organisation;

trait BelongsToOrganisation
{
    /** @var Organisation */
    public $organisation;

    /**
     * Mount the component.
     */
    public function mount(Organisation $organisation): void
    {
        $this->organisation = $organisation;
    }

    /**
     * Handle the "updating" lifecycle hook.
     */
    public function updating(): void
    {
        $this->organisation = Organisation::findOrFail($this->organisation['id']);
    }
}
@calebporzio

This comment has been minimized.

Copy link
Owner

commented Aug 8, 2019

Wondering if we create different lifecycle hooks.

Like ->hydrating and ->dehydrating (these would behave like the old updated/updating)

What do you think?

@calebporzio

This comment has been minimized.

Copy link
Owner

commented Aug 8, 2019

Ok, I added a new lifecycle hook called ->hydrate() that will be like a "mount()", except it will run at the beginning of every Livewire request. (but not the first load -> that's where mount() will run).

It will be available in the next release.

@faustbrian

This comment has been minimized.

Copy link
Contributor Author

commented Aug 8, 2019

Thanks, that got me up and running again. Will be useful for a lot of people to maintain their model state.

@faustbrian faustbrian closed this Aug 8, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.