barryvdh / laravel-ide-helper

IDE Helper for Laravel
MIT License
14.25k stars 1.17k forks source link

::find and ::first not returning correct model #1609

Open juliansteenbakker opened 2 weeks ago

juliansteenbakker commented 2 weeks ago

Versions:

Description:

::find returns Illuminate\Database\Eloquent\Collection|null|TModel ::first returns Illuminate\Database\Eloquent\TValue|null

image

mfn commented 2 weeks ago

Probably because \Illuminate\Database\Eloquent\Builder::find transparently supports finding multiple models by their primary key

pataar commented 2 weeks ago

Actually, it's because generics aren't supported correctly. I tried to fix it on my fork, but it's a bit hard because of the forked phpdoc package that's included.

It should return a TModel, which is defined as template at the top of the Builder class: image

Wat worked for me (it's far from ideal though), is replacing the @mixin \Eloquent for the @mixin Builder<$this> in each model phpdoc.

This gives the following result: image

It can be done using this code snippet (requires the GNU sed version):

find app/Models -type f -name '*.php' -exec sed -i 's/@mixin Eloquent/@mixin \\Illuminate\\Database\\Eloquent\\Builder<\$this>/g' {} +
find app/Models -type f -name '*.php' -exec sed -i 's/@mixin \\Eloquent/@mixin \\Illuminate\\Database\\Eloquent\\Builder<\$this>/g' {} +