You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
while this is definitely a subjective change, I think it provides some significant benefits:
- consistent naming pattern with Laravel internal casts
- prefix helps indicate a cast object, and avoid confusion with names of value objects
- avoids having to alias imports. value objects get the noun, and casts get the prefix.
generally speaking, I think these changes improve readability and consistency.
Copy file name to clipboardExpand all lines: eloquent-mutators.md
+16-16
Original file line number
Diff line number
Diff line change
@@ -669,7 +669,7 @@ $users = User::select([
669
669
Laravel has a variety of built-in, helpful cast types; however, you may occasionally need to define your own cast types. To create a cast, execute the `make:cast` Artisan command. The new cast class will be placed in your `app/Casts` directory:
670
670
671
671
```shell
672
-
php artisan make:cast Json
672
+
php artisan make:cast AsJson
673
673
```
674
674
675
675
All custom cast classes implement the `CastsAttributes` interface. Classes that implement this interface must define a `get` and `set` method. The `get` method is responsible for transforming a raw value from the database into a cast value, while the `set` method should transform a cast value into a raw value that can be stored in the database. As an example, we will re-implement the built-in `json` cast type as a custom cast type:
@@ -682,7 +682,7 @@ namespace App\Casts;
682
682
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
683
683
use Illuminate\Database\Eloquent\Model;
684
684
685
-
class Json implements CastsAttributes
685
+
class AsJson implements CastsAttributes
686
686
{
687
687
/**
688
688
* Cast the given value.
@@ -714,7 +714,7 @@ Once you have defined a custom cast type, you may attach it to a model attribute
714
714
715
715
namespace App\Models;
716
716
717
-
use App\Casts\Json;
717
+
use App\Casts\AsJson;
718
718
use Illuminate\Database\Eloquent\Model;
719
719
720
720
class User extends Model
@@ -727,7 +727,7 @@ class User extends Model
727
727
protected function casts(): array
728
728
{
729
729
return [
730
-
'options' => Json::class,
730
+
'options' => AsJson::class,
731
731
];
732
732
}
733
733
}
@@ -738,28 +738,28 @@ class User extends Model
738
738
739
739
You are not limited to casting values to primitive types. You may also cast values to objects. Defining custom casts that cast values to objects is very similar to casting to primitive types; however, if your value object encompasses more than one database column, the `set` method must return an array of key / value pairs that will be used to set raw, storable values on the model. If your value object only affects a single column, you should simply return the storable value.
740
740
741
-
As an example, we will define a custom cast class that casts multiple model values into a single `Address` value object. We will assume the `Address` value has two public properties: `lineOne` and `lineTwo`:
741
+
As an example, we will define a custom cast class that casts multiple model values into a single `Address` value object. We will assume the `Address` value object has two public properties: `lineOne` and `lineTwo`:
742
742
743
743
```php
744
744
<?php
745
745
746
746
namespace App\Casts;
747
747
748
-
use App\ValueObjects\Address as AddressValueObject;
748
+
use App\ValueObjects\Address;
749
749
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
750
750
use Illuminate\Database\Eloquent\Model;
751
751
use InvalidArgumentException;
752
752
753
-
class Address implements CastsAttributes
753
+
class AsAddress implements CastsAttributes
754
754
{
755
755
/**
756
756
* Cast the given value.
757
757
*
758
758
* @param array<string,mixed> $attributes
759
759
*/
760
-
public function get(Model $model, string $key, mixed $value, array $attributes): AddressValueObject
760
+
public function get(Model $model, string $key, mixed $value, array $attributes): Address
761
761
{
762
-
return new AddressValueObject(
762
+
return new Address(
763
763
$attributes['address_line_one'],
764
764
$attributes['address_line_two']
765
765
);
@@ -773,7 +773,7 @@ class Address implements CastsAttributes
773
773
*/
774
774
public function set(Model $model, string $key, mixed $value, array $attributes): array
775
775
{
776
-
if (! $value instanceof AddressValueObject) {
776
+
if (! $value instanceof Address) {
777
777
throw new InvalidArgumentException('The given value is not an Address instance.');
778
778
}
779
779
@@ -808,7 +808,7 @@ When attributes that are cast to value objects are resolved, they are cached by
808
808
If you would like to disable the object caching behavior of custom cast classes, you may declare a public `withoutObjectCaching` property on your custom cast class:
809
809
810
810
```php
811
-
class Address implements CastsAttributes
811
+
class AsAddress implements CastsAttributes
812
812
{
813
813
public bool $withoutObjectCaching = true;
814
814
@@ -843,7 +843,7 @@ Occasionally, you may need to write a custom cast class that only transforms val
843
843
Inbound only custom casts should implement the `CastsInboundAttributes` interface, which only requires a `set` method to be defined. The `make:cast` Artisan command may be invoked with the `--inbound` option to generate an inbound only cast class:
844
844
845
845
```shell
846
-
php artisan make:cast Hash --inbound
846
+
php artisan make:cast AsHash --inbound
847
847
```
848
848
849
849
A classic example of an inbound only cast is a "hashing" cast. For example, we may define a cast that hashes inbound values via a given algorithm:
@@ -856,7 +856,7 @@ namespace App\Casts;
856
856
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
857
857
use Illuminate\Database\Eloquent\Model;
858
858
859
-
class Hash implements CastsInboundAttributes
859
+
class AsHash implements CastsInboundAttributes
860
860
{
861
861
/**
862
862
* Create a new cast class instance.
@@ -893,7 +893,7 @@ When attaching a custom cast to a model, cast parameters may be specified by sep
893
893
protected function casts(): array
894
894
{
895
895
return [
896
-
'secret' => Hash::class.':sha256',
896
+
'secret' => AsHash::class.':sha256',
897
897
];
898
898
}
899
899
```
@@ -922,7 +922,7 @@ Objects that implement the `Castable` interface must define a `castUsing` method
922
922
namespace App\ValueObjects;
923
923
924
924
use Illuminate\Contracts\Database\Eloquent\Castable;
925
-
use App\Casts\Address as AddressCast;
925
+
use App\Casts\AsAddress;
926
926
927
927
class Address implements Castable
928
928
{
@@ -933,7 +933,7 @@ class Address implements Castable
933
933
*/
934
934
public static function castUsing(array $arguments): string
0 commit comments