vincent15000's avatar

Custom Eloquent cast types ?

Hello,

Since Laravel 7, it's possible to create custom Eloquent cast types, for example an Email type.

I understand that it's useful to be sure that a given property in a model is of the right type, for example the Email type.

I never used custom Eloquent cast types and I don't really understand why it's useful and in which cases it's useful.

The only interesting thing is the one given as example in the documentation : it's possible to have a unique address field with two distinct lines. But why not simply add 2 distinct fields in the model like 2 distinct fields in the database table ?

And why would I need to have value objects ?

Thanks for your explanations ;).

V

0 likes
9 replies
jlrdw's avatar

I would store an address the regular two different fields way. Also read (search for) @tray2 answers on storing Json and array data, generally not a good idea.

3 likes
vincent15000's avatar

@Tray2 I already read your post, it's very complete and very instructive.

But all this doesn't really answer to my question about custom Eloquent casting or I'm not able to understand the link between both concepts.

I'm looking for a concrete example which could justify using a custom cast, but I don't find any. Furthermore in a video from Jeffrey Way where he presents this functionality, even if the model has a custom Eloquent cast for an address with two properties (address 1 and address 2), in the database there are two fields (if I remember).

1 like
jlrdw's avatar

@vincent15000 it seems custom Eloquent casting is storing an array. Frankly I wouldn't worry about custom Eloquent casting, just store in normal fields.

To really understand casting, do a small java ee or jakarta ee project and believe me you will learn casting. There you have to cast correctly. Also c# you have to cast. PHP is a loose language, meaning you post "123" and if the field in mysql is INT it handles this, not so in many other languages, even in python you have to cast certain things correctly. But just my suggestion.

2 likes
vincent15000's avatar

@jlrdw I know casting and effectively as you say other languages are more strict about the different types to store in a variable. I have coded in C and C++ in the past ;).

According to your answer, the only real interest to create custom cast in Laravel would be to assure to handle properties with the right type in a model.

So it's a kind of tool to let PHP be more strict with the types inside a Laravel application ?

2 likes
Tray2's avatar

@vincent15000 I know you did, we chatted about it before. The reply was for @jlrdw so he could save the link in his favorites, I mean who doesn't have a ll my posts in their favorites? ;P

2 likes
jlrdw's avatar
jlrdw
Best Answer
Level 75

@vincent15000 also me being old school I code like Taylor and the actual framework I don't code like laravel.

I think the only helper I actually use is the asset helper for displaying images.

Even the mutators and assessors laravel has, I don't use I still like writing my own getters and setters when needed.

2 likes
krisi_gjika's avatar

personally I see two use cases for custom casts, I also used casts to handle enum columns in db but this is now built in.

  • Casting can handle simple field types that exists in your application layer, but not in the language layer. One example of this would be a percentage field representing a discount, you know this field should be > 0 and < 100 and it should be displayed with a %, but nothing on the language level can ensure this. So instead of relying on int|float you can implement a Percentage "type". If you don't cast this value you constantly have to ensure this value is correct and it's being displayed the right way. If your application deals with this kind of fields a lot this can be a pain, where a cast can unify this across several fields/models.

  • Casting can handle complex json fields, that you are sometimes stuck with. From a recent project: let's say we have a complex status field of {'phase1' => {'key1' => true|false|null, 'key2' => true|false|null}, ...}, say the structure is not completely set. We know we will have 4 phases but we don't know the exact keys that will be present, while some keys might be required. But a cast can define those required keys, build a basic structure and fill it with the data in the json field, handling in this process missing/extra keys, implementing methods to modify this structure in a builder pattern unifying our interface so that we don't mess things up.

1 like

Please or to participate in this conversation.