PHPStan complains about @var Builder in Laravel Eloquent scope

1 week ago 9
ARTICLE AD BOX

I'm trying to define a Laravel Eloquent model with a custom scope that filters categories by a related product's vendor_id. Here's my setup:

class Category extends Model { public function productTemplates(): HasMany { return $this->hasMany(ProductTemplate::class, 'category_id'); } #[Scope] protected function byVendor(Builder $query, ?int $vendorId = null): void { $query->whereHas('productTemplates', function (Builder $productQuery): void { /** @var Builder<ProductTemplate> $productQuery */ $productQuery->byVendor($vendorId); }); } } class ProductTemplate extends Model { #[Scope] protected function byVendor(Builder $query, ?int $vendorId = null): void { if ($vendorId) { $query->where('vendor_id', $vendorId); } } }

When I run PHPStan, I get this error:

PHPDoc tag @var with type Illuminate\Database\Eloquent\Builder<App\Models\ProductTemplate> is not subtype of native type Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model>.

How can I fix the PHPDoc or type hinting so PHPStan recognizes the $productQuery type correctly in the scope?

Read Entire Article