Skip to content

Running PHP after a property is updated

Often, when a property is updated, we need to execute some logic to update the value in the session or a database table.

To do so we can use a callback method on the component.
For example, if we want to do something after a property myProp is updated, we need to use the method

public function updatedMyProp(string $value): string
{
    // Do "something", for example
    // update $this->myProp in the database or the session.
    return $value;
}

The updatingPropName() callback method is an example of a Magewire lifecycle hook method. This hook method is only called if the update is done through Magewire, for example via $set(). It is not called if the property is updated using assignment in PHP.

There also is a generic updated($propName, $value) lifecycle method that is called for every property update, even though the property specific one is more common.

More information on such hook methods can be found in the lifecycle hook methods documentation.