Essay Nº 007 — php
Fix phpstan undefined resource property
When using phpstan on resources in Laravel an undefined error message comes up on every property. Why is that? It is because the resource resolves the properties via $this->resource->{'property'} which phpstan does not understand.
This can however be fixed: going explicitly via the resource property or assigning it to a variable resolves the errors.
<?php
// ...
$item = $this->resource;
return [
'id' => $item->id,
// ...
];
See https://masteringlaravel.io/daily/2024-09-16-better-types-in-api-resources to give proper credit to the source that made me try this.
❦