Skip to content

Unable to access pivot table when model has a custom collection #38

@AJenbo

Description

@AJenbo

To get pivots working with custom collections it seems I have to permit that the custom collections can be extended. To avoid having to specify it's type everywhere we use it I do the following to also set it's default type:

/**
 * @template T of Product = Product
 * @extends Collection<int, T>
 */
class ProductCollection extends Collection {}

But now I have several errors elsewhere in the application where the return type appears as:

Dumped type: Luxplus\Core\Database\Model\Products\ProductCollection<int> 

Which then results in the following false positives:

  212    Parameter #1 $callback of method Illuminate\Support\Collection<int,int>::sortBy() expects array<array{string, string}|(callable(int, int):  
         mixed)|string>|(callable(int, int): mixed)|string, Closure(Model\Products\Product): int<0, max> given.

For this code:

            $products = Product::all();
            \PHPStan\dumpType($products);
            $products->sortBy(fn(Product $product) => $sortMap[$product->id]);

Could you help clarify if I'm doing something wrong or is this an actual bug?

Without the default i get expectedly get a lot of Method ProductCollection::getProducts() return type with generic class ProductCollection does not specify its types: TModel

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions