-
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
collection keyBy / groupBy #19
Comments
Hello! The framework added better generics in laravel/framework#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
<laravel/framework#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
<#19 (comment)>,
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...
The text was updated successfully, but these errors were encountered: