corcel
corcel copied to clipboard
Custom aliases and casts for post meta not working
- Corcel Version: 5.0
- Framework Name & Version: Laravel 8
- PHP Version: 8.0
- Database Driver & Version: MariaDB 10.6
Description:
When defining the $aliases on a custom model, the meta fields aren't loaded correctly and the $casts seem to be ignored.
~~(On a side note, default post aliases don't seem to be inherited correctly, like title or slug etc. I need to define them all as well.)~~ Fixed by adding use Corcel\Concerns\Aliases; to custom model.
Setting the meta data keys in $appends works correctly, but the $aliases seem to be ignored.
Maybe I'm missing something here?
Steps To Reproduce:
- Create a custom model and extend
Corcel\Model\Post - Add a custom meta field called
channel_failuresand set it to a value of10. - Define a custom meta field in
$appendsand make an alias in$aliases, add field to$casts.protected $casts = [ 'failures' => 'int', // doesn't work 'channel_failures' => 'int', // doesn't work ]; protected $appends = [ 'failures', // doesn't work 'channel_failures', // works ]; protected static $aliases = [ 'failures' => 'channel_failures', // doesn't work ]; - Try it and see that it doesn't work as expected:
MyPostType::first()->failures; // null MyPostType::first()->channel_failures; // "10" <- not being cast to int
After diving into the code, I found that meta field aliases need to be defined as an array:
protected static $aliases = [
'failures' => ['meta' => 'channel_failures'], // works!
];
Still no luck with the $casts yet