laravel-ide-helper icon indicating copy to clipboard operation
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)

Open amackiewicz opened this issue 5 years ago • 1 comments

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:

  1. enable camelCase support for models properties in Laravel (if its not set already)

  2. add $dates property in model and fillup with db column name typed in snakeCase, for example:

protected $dates = [
    'employmentStartDate', 
];
  1. 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
  1. try to execute format method on model property for example in controller:
$employee->employmentStartDate->format('c');
  1. see in IDE that "format" method not found as employmentStartDate is a string

  2. review _ide_helper_models.php to make sure:

$ cat _ide_helper_models.php | grep employmentStartDate
 * @property string|null $employmentStartDate
  1. (from here we will hack around that issue) Change model $dates property to snake_case:
protected $dates = [
    'employment_start_date',  // ide-helper ignoring camelCase
];
  1. 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
  1. try to execute format method on model property for example in controller:
$employee->employmentStartDate->format('c');
  1. see in IDE that everything is ok :)

  2. 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)

amackiewicz avatar Sep 18 '20 05:09 amackiewicz

Just curious if there is any fix for this issue?

LiamKarlMitchell avatar Jul 07 '21 05:07 LiamKarlMitchell