Closed makroxyz closed 2 months ago
Hello!
The framework added better generics in https://github.com/laravel/framework/pull/52787, so now PHPStan can determine what the collection key type should be. However, this only works for closures:
$a = MyModel::all()->keyBy('id'); // Collection<(int|string), MyModel>
$a = MyModel::all()->keyBy(fn ($m) => $m->id); // Collection<int, MyModel>
If you want to just use a string, then you can ignore the error locally (or globally):
/** @phpstan-ignore argument.templateType */
$a = MyModel::all()->keyBy('id');
This is the best we can do with just PHPDocs, but I'm thinking about writing an extension in Larastan that will be able to determine the type even if an array/string is passed
Thank you very much!!
Il mer 25 set 2024, 15:50 Caleb White @.***> ha scritto:
Hello!
The framework added better generics in laravel/framework#52787 https://github.com/laravel/framework/pull/52787, so now PHPStan can determine what the collection key type should be. However, this only works for closures:
$a = MyModel::all()->keyBy('id'); // Collection<(int|string), MyModel>$a = MyModel::all()->keyBy(fn ($m) => $m->id); // Collection<int, MyModel>
If you want to just use a string, then you can ignore the error locally (or globally):
/* @phpstan-ignore argument.templateType /$a = MyModel::all()->keyBy('id');
This is the best we can do with just PHPDocs, but I'm thinking about writing an extension in Larastan that will be able to determine the type even if an array/string is passed
— Reply to this email directly, view it on GitHub https://github.com/calebdw/larastan/issues/19#issuecomment-2374149695, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAPZLPN2RDHDQ6XDCFVC5TDZYK5RNAVCNFSM6AAAAABOZ43SCGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNZUGE2DSNRZGU . You are receiving this because you authored the thread.Message ID: @.***>
Description
Unable to resolve the template type TNewKey in call to method Illuminate\Support\Collection<int,App\Models\MyModel>::keyBy()
Laravel code where the issue was found
Sorry... but I can't understand how to solve this...