Laravel 13.33 New Published on

Reading back the columns the database computes with #[Refreshes]

A model marked #[Refreshes] reads back, after every write, the columns the database fills in itself: default value, generated column.

Topics covered: Eloquent

New in Laravel 13.33. When the database fills in a column itself (a default value, a generated column, a trigger), the model doesn't know it after save(): you have to read it again with refresh(), which reloads every column. The #[Refreshes] attribute names the columns to read back after every write.

Before

$order = Commande::create($data);
$order->refresh(); // otherwise $order->total is null

Since Laravel 13.33

#[Refreshes(['status', 'total'])]
class Commande extends Model
{
}

After an insert, an update or an increment(), Eloquent reads these columns back, and only them, in a single query. You can also declare them in the protected array $refreshes property.

Your task

The commandes table (orders) computes total (quantity × unit_price, a generated column) and gives status the default value pending. The API returns the order right after creating or updating it, but these fields are wrong: null for both on creation, the old total after an update. Fix the Commande model with #[Refreshes], without touching the controller.

Where the feature comes from: the original pull request.