laravel-ide-helper icon indicating copy to clipboard operation
laravel-ide-helper copied to clipboard

Eloquent whereIn returns Eloquent\Builder not Query\Builder

Open mycarrysun opened this issue 4 years ago • 4 comments

Versions:

  • ide-helper Version: 2.8.2
  • Laravel Version: 8.18.1
  • PHP Version: 8.0.0

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

mycarrysun avatar Dec 19 '20 14:12 mycarrysun

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

mfn avatar Dec 20 '20 21:12 mfn

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.

mycarrysun avatar Dec 23 '20 00:12 mycarrysun

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

FatBoyXPC avatar Jan 27 '21 20:01 FatBoyXPC

I guess while not extra helpfull me too

jfinstrom avatar Nov 02 '21 18:11 jfinstrom