laravel-ide-helper: Duplicate property creation every time it runs

Versions:

  • ide-helper Version: v2.13.0
  • Laravel Version: v9.52.0
  • PHP Version: 8.2

Description:

After updating to the latest version, every time I run the php artisan ide-helper:models --write command, the property-read in the model is added again, causing duplicate values in the model.

/**
 * App\Models\Branch
 *
 * @property int $id
 * @property int $company_id
 * @property string $name
 * @property string|null $phone
 * @property bool $active
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\QrMenu> $activeQrMenus
 * @property-read int|null $active_qr_menus_count
 * @property-read \Illuminate\Database\Eloquent\Collection<int, User> $admins
 * @property-read int|null $admins_count
 * @property-read \App\Models\Company $company
 * @method static \Database\Factories\Company\BranchFactory factory($count = null, $state = [])
 * @method static \Illuminate\Database\Eloquent\Builder|Branch newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Branch newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Branch query()
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\QrMenu> $activeQrMenus
 * @property-read \Illuminate\Database\Eloquent\Collection<int, User> $admins
 * @mixin \Eloquent
 */

The activeQrMenus and admins values ​​here are repeating.

There are many models in the project, but this error only occurs in 5 models.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 12
  • Comments: 22 (1 by maintainers)

Most upvoted comments

@barryvdh can you help please?)

I acknowledge this seems buggy, so we definitely appreciate if someone steps up and looks into fixing this.

Btw, for those affected: can you use --reset?

(I realized I’m not affected by this bug, because my pipeline uses ide-helper:models --write --reset)

I can’t use --reset because I’ve defined some model getters using Laravel’s Attribute, and when I add --reset, it deletes the manually added properties.

FYI I needed to fix this because it caused my CI to fail every time. How I fixed is by changing the command to php artisan ide-helper:models --no-interaction --write --reset. The --reset flag resets the lookup and re-populates the models.

+1 can we get that dependency updated?

Same problem here. Just a bit of knowledge since it’s not mentioned in this thread. In my case, it only happens if there was something “irregular” with the naming convention.

For example the model is User but the relation is called admins. Or if it’s two words like payment_logs. Or if it’s 1 word, but you’re not using standard column naming like user_id for a foreign key on users

+1 on this issue

Same issue. However, I found out, that use_generics_annotations set to true will produce duplicates like this

I ran it three times with this setting and it always seem to duplicate the @property-read from HasApiTokens and Notifiable in my default User model. Switching the generics annotations off fixes this issue (requires deleting the previous model docBlock)

* @property-read \Illuminate\Notifications\DatabaseNotificationCollection<int, \Illuminate\Notifications\DatabaseNotification> $notifications
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Laravel\Sanctum\PersonalAccessToken> $tokens
+* @property-read \Illuminate\Notifications\DatabaseNotificationCollection<int, \Illuminate\Notifications\DatabaseNotification> $notifications
+* @property-read \Illuminate\Database\Eloquent\Collection<int, \Laravel\Sanctum\PersonalAccessToken> $tokens
+* @property-read \Illuminate\Notifications\DatabaseNotificationCollection<int, \Illuminate\Notifications\DatabaseNotification> $notifications
+* @property-read \Illuminate\Database\Eloquent\Collection<int, \Laravel\Sanctum\PersonalAccessToken> $tokens

Related block:

https://github.com/barryvdh/laravel-ide-helper/blob/4dc20b028adaeadf603860f179f09da5aa21f5b5/src/Console/ModelsCommand.php#L748

Both mentioned traits indeed extend from Relation, so maybe it just can’t match the correct type for collections of those relations.

yes, it is always on Collection

Is it always on Collections?