larastan icon indicating copy to clipboard operation
larastan copied to clipboard

Eloquent builder whereRelation removes type from Collection value

Open schonhoff opened this issue 3 years ago • 1 comments

  • Larastan Version: 2.2.0
  • Laravel Version: 9.30
  • --level used: 6
  • Pull request with failing test: Hopefully my example helps to reproduce. If not I will create an example repository.

Description

Hello,

I faced an error after I changed my Level from 5 to 6.

Method App\Services\ArticleService::getFieldsFromOrder() should return
         Illuminate\Database\Eloquent\Collection<int, App\Models\Field> but returns
         Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>.

I tried a lot of things with the code provide below. After I commented out line after line the error was only shown if the ->whereRelation code is present. I hope there is a possibility to change the behavior.

Laravel code where the issue was found

    /**
     * @param  \App\Models\Order  $order
     *
     * @return \Illuminate\Database\Eloquent\Collection<int, \App\Models\Field>
     */
    private function getFieldsFromOrder(Order $order): \Illuminate\Database\Eloquent\Collection
    {
        return \App\Models\Field::query()
            ->whereIn('name_attribute', ['hello_world'])
            ->whereRelation('orderTypes', 'order_types.id', '=', 1)
            ->get(['fields.id', 'fields.name_attribute']);
    }

schonhoff avatar Sep 20 '22 10:09 schonhoff

I second that.

szepeviktor avatar Sep 20 '22 10:09 szepeviktor