Skip to content

Commit

Permalink
document withoutObjectCaching
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 20, 2022
1 parent 76ee584 commit 2478a30
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions eloquent-mutators.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,36 @@ public function address(): Attribute
}
```

When returning value objects from accessors, any changes made to the value object will automatically be synced back to the model before the model is saved. This is possible because Eloquent retains instances returned by accessors so it can be return the same instance each time the accessor is invoked:

use App\Models\User;

$user = User::find(1);

$user->address->lineOne = 'Updated Address Line 1 Value';
$user->address->lineTwo = 'Updated Address Line 2 Value';

$user->save();

If you would like to disable the object caching behavior of attributes, you may invoke the `withoutObjectCaching` method when defining the attribute:

```php
/**
* Interact with the user's address.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute
*/
public function address(): Attribute
{
return (new Attribute(
get: fn ($value, $attributes) => new Address(
$attributes['address_line_one'],
$attributes['address_line_two'],
),
))->withoutObjectCaching();
}
```

<a name="defining-a-mutator"></a>
### Defining A Mutator

Expand Down

0 comments on commit 2478a30

Please sign in to comment.