Laravel-Backpack / community-forum

A workspace to discuss improvement and feature ideas, before they're actually implemented.
28 stars 0 forks source link

[Feature Request] Add casts to translatable (fake) columns #3

Open OliverZiegler opened 6 years ago

OliverZiegler commented 6 years ago

At this point we have translatable fake fields and are able to add the fakes to our model.

What would be nice is, if we could also cast our translatable fake fields like normal laravel ones. This could help us on improving usage of fakes I think.

So think of following model:

class Company {
    use CrudTrait;
    use HasTranslations;
    protected $fillable = [
        'address',
        'postal'
    ];

    protected $casts = [
        'address',
        'postal'
    ];

    protected $translatable = [
        'address',
    ]
}

where we save different addresses for different languages and one address where all the mailing should go to. Each address would contain a street, zip, `city... (I'm really bad at good, easy examples, so don't bother with logic šŸ˜‰ ).

For our frontend it would be nice to have $instance->address->street rather than only $instance->street as it gets more readable and removes the double naming for street. So we could easily have $instance->address->street and $instance->postal->street without any issues.

Starting to make address_street and postal_street blows up data and code length but is currently necessary.

So we could overwrite the translatable stuff to provide casting of translated attributes (this can also be handy with, int, bool, timestamp). In my opinion this would lead to easier maintainability, cleaner code and is some really nice to have feature.

In long view (BP 4 perhaps) we could refactor the fields to use the column-namespaces fake attributes instead of the directly added and get rid of the direct attribute appliance and so naming conflicts.

Adding another options, we can still apply stuff like extras->firstname, extras->lastname for convenience.

Perhaps this should get another name than fake fields... I'm not sure... let's just discuss here :)

What du you guys think?

tabacitu commented 5 years ago

Hi @OliverZiegler ,

I agree - castable fake fields sounds like a good feature to me too. 4.0 might be the best time to include it.

In long view (BP 4 perhaps) we could refactor the fields to use the column-namespaces fake attributes instead of the directly added and get rid of the direct attribute appliance and so naming conflicts.

Sorry - could you please give more details on this, what do you mean?

Perhaps this should get another name than fake fields... I'm not sure... let's just discuss here :)

I agree. Open to suggestions on better naming :-)

Thank you for raising the issue. Cheers!