laravel

How with larastan fix errors in Resource file?


On laravel site Running larastan on LanguageResource.php file with lines :

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class LanguageResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id'     => $this->id,
            'locale' => $this->locale,
            'prefix' => $this->prefix,
        ];
    }

}

I got next errors :

  19     Access to an undefined property App\Http\Resources\LanguageResource::$id.
     💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property
  20     Access to an undefined property App\Http\Resources\LanguageResource::$locale.
     💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property
  21     Access to an undefined property App\Http\Resources\LanguageResource::$prefix.
    💡 Learn more: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property

Usually when dealing similar errors with mopels I use getAttribute method, but it does not work in this case, as $this is an Resource object here. Can I got model in this resource or to salve in it some other case ?

"laravel/framework": "^10.8",
"nunomaduro/larastan": "^2.0",

Thanks in advance!


Solution

  • Try specifying JSON Resource classes as "Universal Object Crates", by adding this to your phpstan.neon file:

    parameters:
        universalObjectCratesClasses:
            - Illuminate\Http\Resources\Json\JsonResource