Skip to content

Commit 5070d92

Browse files
committed
document compare
1 parent 85d59fb commit 5070d92

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

eloquent-mutators.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- [Array / JSON Serialization](#array-json-serialization)
1616
- [Inbound Casting](#inbound-casting)
1717
- [Cast Parameters](#cast-parameters)
18+
- [Comparing Cast Values](#comparing-cast-values)
1819
- [Castables](#castables)
1920

2021
<a name="introduction"></a>
@@ -926,6 +927,33 @@ protected function casts(): array
926927
}
927928
```
928929

930+
<a name="comparing-cast-values"></a>
931+
### Comparing Cast Values
932+
933+
If you would like to define how two given cast values should be compared to determine if they have been changed, your custom cast class may implement the `Illuminate\Contracts\Database\Eloquent\ComparesCastableAttributes` interface. This allows you to have fine-grained control over which values Eloquent considers changed and thus saves to the database when a model is updated.
934+
935+
This interface states that your class should contain a `compare` method which should return `true` if the given values are different:
936+
937+
```php
938+
/**
939+
* Determine if the given values are equal.
940+
*
941+
* @param \Illuminate\Database\Eloquent\Model $model
942+
* @param string $key
943+
* @param mixed $firstValue
944+
* @param mixed $secondValue
945+
* @return bool
946+
*/
947+
public function compare(
948+
Model $model,
949+
string $key,
950+
mixed $firstValue,
951+
mixed $secondValue
952+
): bool {
953+
return $firstValue !== $secondValue;
954+
}
955+
```
956+
929957
<a name="castables"></a>
930958
### Castables
931959

0 commit comments

Comments
 (0)