spatie / laravel-query-builder

Easily build Eloquent queries from API requests
https://spatie.be/docs/laravel-query-builder
MIT License
4k stars 392 forks source link

PHPStan doesn't know the return type of QueryBuilder #953

Open genesiscz opened 1 month ago

genesiscz commented 1 month ago

Hello,

I am not sure if I am missing something but let me explain.

When I do

        $formFields = FormField::query()->where("id", $formField->id);
        $a = $formFields->firstOrFail();
        \PHPStan\dumpType($a);

        $b = QueryBuilder::for(FormField::query()->where('id', $formField->id))
            ->allowedIncludes(['formFieldSelectItems'])
            ->get();

        \PHPStan\dumpType($b);

I recieve

 45     Dumped type: App\Models\Forms\FormField                                                                                                                                                            
  51     Dumped type: Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>       

I feel like the collection should be automatically inferred or somehow working without PHPDocs, shouldn't it? I have this line in FormField:

@method static Builder<FormField>|FormField query()

Am I missing something or wasn't this thought of ?

Also, I think that the QueryBuilder should be generic in the first place, shouldn't be?