sandaru1's avatar

Attribute casting

I'm working on a visual studio code extension to add Eloquent model attributes automatically,($primaryKey, $fillable, $casts, etc) depending on the database table.

I'm updating the list of attribute casting types and trying to match them to MySQL column types. Is there any point of casting to float, double, real and string? Also, is there a default MySQL equivalent of collection?

0 likes
2 replies
click's avatar
click
Best Answer
Level 35

Is there any point of casting to float, double, real and string?

Yes, casting also works without the database.

// set a string
$invoice->cents = "10000"; 

// returns a string 
var_dump($invoice->cents); 

$casts = [
     'cents' => 'int',
];

// set a string
$invoice->cents = "10000"; 

// returns an integer 
var_dump($invoice->cents); 

Please or to participate in this conversation.