vkovic / laravel-custom-casts

Make your own custom cast type for Laravel model attributes
MIT License
219 stars 21 forks source link

Is this the same as laravel custom cast attributes in laravel 7? #21

Open codeitlikemiley opened 4 years ago

codeitlikemiley commented 4 years ago

I would like to know if this is the same as laravel custom cast attributes in laravel 7... Is this the same repo merge in the laravel framework or different? What are the difference with this repo to the one being used by the framework thanks

vkovic commented 4 years ago

Interesting question here ... So, basically those 2 has the same purpose, and very similar flow.

Code in Laravel 7 is not taken / merged from this repo, or vice versa.

My package was made while Laravel 5.7 was actual, and I provided support further for Laravel 6.x versions.

There are couple of differences I could notice after reading a bit on new custom casts feature L7 provides us, and those are:

Passing parameters to custom casts constructor

While defining custom casts, L7 has support for passing parameters directly to our custom casts class constructor, while my package lacks this feature.

Example:

/**
 * The attributes that should be cast.
 *
 * @var array
 */
protected $casts = [
    'secret' => Hash::class.':sha256', // <== "sha256" string is passed directly to custom cast class constructor
];

Handling model events

For convenience, I implemented direct event handling in custom cast classes, so it's very easy to react to standard eloquent model events. Read more about it [here (https://github.com/vkovic/laravel-custom-casts#handling-model-events)

This could be probably accomplished easy, but with bit more code in L7 default custom classes.

Conclusion

If your're using Laravel version < 7 you could use my package, and if Laravel version is >= 7 than using Laravel default custom casts would probably be a better choice.


There are probably more differences but I can't see them currently. I'm still heavily using L5 and L6 versions. So if anyone can think of some more, please post them here so we have it all in one place.