jn-jairo / laravel-eloquent-cast
Cast for Laravel Eloquent.
Requires
- php: ^8.1
- illuminate/support: ^8.83|^9.0|^10.0|^11.0|^12.0
- jn-jairo/laravel-cast: ^2.0.2
Requires (Dev)
- orchestra/testbench: ^6.24|^7.0|^8.0|^9.0|^10.0
- pestphp/pest: ^1.22|^2.34|^3.7.4
- php-decimal/php-decimal: ^1.1
README
Cast for Laravel Eloquent
This package extends the built-in attribute casting with the jn-jairo/laravel-cast package.
Version Compatibility
Laravel | Eloquent Cast |
---|---|
5.8.x | 1.x |
6.x | 1.x |
7.x | 2.x |
8.x | 3.x |
9.x | 3.x |
10.x | 3.x |
11.x | 3.x |
12.x | 3.x |
Installation
You can install the package via composer:
composer require jn-jairo/laravel-eloquent-cast
Usage
Use the trait \JnJairo\Laravel\EloquentCast\HasAttributesCast
in a \Illuminate\Database\Eloquent\Model
.
In the $casts
property of the model set the attribute name as the key and the type:format as the value.
<?php namespace App; use Illuminate\Database\Eloquent\Model; use JnJairo\Laravel\EloquentCast\HasAttributesCast; class Foo extends Model { use HasAttributesCast; protected $casts = [ 'uuid' => 'uuid', 'is_admin' => 'boolean', 'created_at' => 'datetime:Y-m-d H:i:s', ]; }
The attribute is casted for a PHP
type in the getter and/or with a suffix, for a database
type in the setter, and for a json
type when serialized to array
/json
.
Example
$foo = new Foo(); $foo->uuid = '72684d25-b173-468d-8d45-2a10b2cc3e9f'; $foo->is_admin = 1; $foo->created_at = '2000-01-01 00:00:00'; print_r(gettype($foo->uuid)); // string print_r(get_class($foo->uuid_)); // Ramsey\Uuid\Uuid var_dump($foo->is_admin); // bool(true) print_r($foo->created_at); Illuminate\Support\Carbon Object ( [date] => 2000-01-01 00:00:00.000000 [timezone_type] => 3 [timezone] => UTC )
Configuration
Publish the configuration to config/eloquent-cast.php
.
php artisan vendor:publish --provider=JnJairo\\Laravel\\EloquentCast\\EloquentCastServiceProvider
In the default configuration the attribute is casted for a PHP
type in the getter and with the _
suffix, except for the type uuid
which is casted only with the _
suffix,
because to cast a primary/foreign key directly in the getter brakes the relations between the eloquent models.
License
The MIT License (MIT). Please see License File for more information.