laracoft's avatar

Is a castable supposed to exists as a field in the table?

Referring to this example https://laravel.com/docs/10.x/eloquent-mutators#castables

Is address supposed to exists as a field in the database table?

0 likes
7 replies
jlrdw's avatar

The casting is in eloquent. You are taking address from the database and casting as you need, for display or whatever.

laracoft's avatar

My model was misbehaving because of some overridden behavior, thus leading to this question.

But I'm still not sure if address must exists as a field, but in my case, it exists as a field.

krisi_gjika's avatar

@laracoft if it does not exist as a field, you are not really casting. You are just creating an attribute.

laracoft's avatar

@krisi_gjika so in the example, I need to have address, address_line_one and address_line_two in my table?

Doesn't make much sense to me. Is there a real world use case for something like this?

krisi_gjika's avatar

@laracoft from my understanding address_line_one and address_line_two exist inside the address column.

Basically the example is showing an alternative to casting this column as a common array, into something that makes more sense for your domain and has defined fields.

Snapey's avatar

@laracoft no, in this example a new Address class is created which contains the first and second lines as properties. Its not an address column

Please or to participate in this conversation.