Default Eloquent Cast #265
Replies: 4 comments 1 reply
-
Hi @aaronflorey 👍 if I understand your question/idea I think the as a result, the default value should be created only by the model namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
/**
* The model's default values for attributes.
*
* @var array
*/
protected $attributes = [
'delayed' => false,
];
}
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'delayed' => 'boolean',
]; |
Beta Was this translation helpful? Give feedback.
-
Hey @Nir-An, Thanks for the reply! Yeah, that's one way to do it, but unfortunately it's not a great solution for this package since you can't use expressions in array properties. I can set the attributes to an empty array to achieve the same effect but not ideal. Thanks |
Beta Was this translation helpful? Give feedback.
-
Any ideas how you would implement such functionality? At the moment not completely against to it but I don't know how you would pull it off. |
Beta Was this translation helpful? Give feedback.
-
in the Database, to make your life easier I encourage you to keep an empty putting 'dto' => '{}' in $attributes on the model should be better solution. |
Beta Was this translation helpful? Give feedback.
-
Currently when using a DTO as a cast in Eloquent, if the column is empty, it won't cast the object.
I'm wanting it to default to an empty DTO if the column is empty, would a PR be accepted to add this functionality possibly through an Attribute?
#[EloquentCastDefault]
or somethingBeta Was this translation helpful? Give feedback.
All reactions