laravel-ide-helper
laravel-ide-helper copied to clipboard
Eloquent model's $dates values are ignored when written in camelCase (with model_camel_case_properties set to true)
Versions:
- ide-helper Version: 2.8.1
- Laravel Version: 7.28.3
- PHP Version: 7.3.19
Description:
Brief: Equolent models $dates camelCase values are ignored by ide-helper with model_camel_case_properties' => true
Full: Eloquent allows to define model properties which are cast to Carbon instance by adding their names into models protected property $dates, ex:
protected $dates = [
'employmentStartDate',
];
When using camelCase notation for properties and enabled camelCase in ide-helper.php config ('model_camel_case_properties' => true,) generated properties are casted to string instead of Carbon instance.
For set $dates properly, compatible with ide-helper with camelCase enabled we need to type them snake_case, ex:
protected $dates = [
'employment_start_date',
];
Steps To Reproduce:
-
enable camelCase support for models properties in Laravel (if its not set already)
-
add $dates property in model and fillup with db column name typed in snakeCase, for example:
protected $dates = [
'employmentStartDate',
];
- refresh whole ide-helper config:
$ php artisan clear-compiled
$ php artisan ide-helper:generate
$ php artisan ide-helper:models --write-mixin
$ php artisan ide-helper:meta
- try to execute format method on model property for example in controller:
$employee->employmentStartDate->format('c');
-
see in IDE that "format" method not found as employmentStartDate is a string
-
review _ide_helper_models.php to make sure:
$ cat _ide_helper_models.php | grep employmentStartDate
* @property string|null $employmentStartDate
- (from here we will hack around that issue) Change model $dates property to snake_case:
protected $dates = [
'employment_start_date', // ide-helper ignoring camelCase
];
- refresh whole ide-helper config:
$ php artisan clear-compiled
$ php artisan ide-helper:generate
$ php artisan ide-helper:models --write-mixin
$ php artisan ide-helper:meta
- try to execute format method on model property for example in controller:
$employee->employmentStartDate->format('c');
-
see in IDE that everything is ok :)
-
review _ide_helper_models.php to make sure:
$ cat _ide_helper_models.php | grep employmentStartDate
* @property \Illuminate\Support\Carbon|null $employmentStartDate
Notes
- Tested property was nullable (if it brings any value)
Just curious if there is any fix for this issue?