barryvdh / laravel-ide-helper

IDE Helper for Laravel
MIT License
14.07k stars 1.16k forks source link

Eloquent whereIn returns Eloquent\Builder not Query\Builder #1128

Open mycarrysun opened 3 years ago

mycarrysun commented 3 years ago

Versions:

Description:

The generated whereIn method for the Eloquent mixin has \Illuminate\Database\Query\Builder for the return object. When actually using the whereIn method on a model it actually returns \Illuminate\Database\Eloquent\Builder. I think the auto generated doc block should be changed to have the typehint of Eloquent builder instead.

Steps To Reproduce:

class Contact extends Model {}

class User extends Model
{
  public function groupContacts(): Contact|\Illuminate\Database\Query\Builder
  {
    return Contact::whereIn('user_id', [1, 2, 3]);
  }
}

Now running this will produce an error (generate the models helper first with php artisan ide-helper:models):


$group_builder = User::first()->groupContacts();
// TypeError: App\Models\User::groupContacts(): Return value must be of type App\Models\Contact|Illuminate\Database\Query\Builder, Illuminate\Database\Eloquent\Builder returned
mfn commented 3 years ago

whereIn isn't the only "affected" one, I mean I can see the same with whereNull and probably lots of others 🤷‍♀️

mycarrysun commented 3 years ago

Yea - I hadn't tested the other ones so wasn't sure what to post here but figured they could all be annotating the wrong return type.

FatBoyXPC commented 3 years ago

I'm having the same problem, but I experienced it through FooModel::whereIn()->get() returning Illuminate\Support\Collection instead of Illuminate\Database\Eloquent\Collection which should be returned from Illuminate\Database\Eloquent\Builder::get().

jfinstrom commented 2 years ago

I guess while not extra helpfull me too