Skip to content

[13.x] Add freshForUpdate() method to Eloquent models - #61497

Closed
imKenjo18 wants to merge 1 commit into
laravel:13.xfrom
imKenjo18:feat/fresh-for-update
Closed

[13.x] Add freshForUpdate() method to Eloquent models#61497
imKenjo18 wants to merge 1 commit into
laravel:13.xfrom
imKenjo18:feat/fresh-for-update

Conversation

@imKenjo18

Copy link
Copy Markdown

Following the addition of refreshForUpdate() in #61247, this PR introduces freshForUpdate() to Eloquent models as a fresh() counterpart.

Because refreshForUpdate() automatically re-loads all currently loaded relationships on the model, obtaining a fresh, locked instance without carrying over or re-querying those relations currently requires writing $model->unsetRelations()->refreshForUpdate() or falling back to manual queries like Model::query()->lockForUpdate()->find(...).

freshForUpdate() provides a clean shorthand that starts with an empty relations state (or only the relations explicitly requested):

// Before:
$order->unsetRelations()->refreshForUpdate();
// or:
$order = Order::query()->lockForUpdate()->find($order->id);

// After:
$order = $order->freshForUpdate();
// Or eager-loading only what you need:
$order = $order->freshForUpdate('items');

@taylorotwell

Copy link
Copy Markdown
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants