Skip to content

Running PHP Code after Magewire Property Updates

Magewire lets you run PHP logic automatically whenever a component property changes. This is handy when you need to persist the updated value to a database, save it in the session, or trigger side effects like recalculating totals.

Using the updated Callback Method in Magewire

Magewire provides a naming-convention-based callback for reacting to property changes. When a property called myProp is updated through Magewire (for example via $set()), Magewire will automatically call a method named updatedMyProp() on your component if it exists.

Here's what that looks like:

// Magewire calls this method automatically after the "myProp" property is updated.
// The naming convention is: updated + PascalCase property name.
public function updatedMyProp(string $value): string
{
    // Run your logic here, for example
    // persist $this->myProp to the database or session.

    // To change the stored value, assign it and return it.
    $this->myProp = trim($value);

    return $this->myProp;
}

Magewire 1 stores the return value of the callback as the property's final value. Magewire 3 ignores the return value and keeps what the callback assigned, so assigning and returning the value works on both release lines. See Lifecycle Hooks in the Magewire 3 docs.

Only triggered by Magewire updates

The updatedMyProp() callback is only called when the property is updated through Magewire (for example via $set() in Alpine.js or a Magewire action). Direct PHP assignment like $this->myProp = 'new value' does not trigger the callback.

Generic updated() Callback for All Magewire Properties

Magewire also provides a generic updated() lifecycle method that fires for every property update on the component, regardless of which property changed. The property-specific callback (like updatedMyProp()) is more common, but the generic version is useful when you want centralized handling for all property changes.

The signature is updated(string $prop, $value): the property name comes first. Components in v1 mode get the Magewire 1 argument order.

The signature is updated($value, $prop): the new value comes first.

Both of these callbacks are Magewire lifecycle hook methods. You can find the full list of available hooks, including updating() variants that fire before the value is applied, in the lifecycle hook methods documentation.