Larastan: Access to an undefined property

Created on 22 May 2020  路  6Comments  路  Source: nunomaduro/larastan

Latest laravel and default settings

Many reports of "Access to an undefined property IlluminateDatabaseEloquentModel::$attribute."

Most helpful comment

The first problem can be solved by adding the following PHPDoc annotation to your resource class:

/** @extends JsonResource<User> */

as described here.

The second problem can be resolved by changing it to either

$dpia = request()->route('dpias');

or

$dpia = app('request')->route('dpias');

The last problem is expected behavior, because Model does not have an id by definition. You can either ignore it by adding e.g. // @phpstan-ignore-next-line, or you could create a custom Model class:

/** 
 * @property int $id
 */
class MyCustomModel extends Model {}

and use that as a typehint.

All 6 comments

Hi,

Can you share any code sample where this error happens? Also please follow the issue template for new issues.

That would make it possible for us to help you.

I'm seeing it about all of my resources and their fields (which are standard) i.e.

Access to an undefined property App\Http\Resources\UserResource::$first_name.
namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'first_name'     => $this->first_name,
            ...

Some other places like service providers:

Providers/RouteServiceProvider.php

  101    Access to an undefined property Illuminate\Foundation\Application::$request.
  157    Access to an undefined property Illuminate\Foundation\Application::$request.
  164    Access to an undefined property Illuminate\Foundation\Application::$request.
        Route::bind('risks', function($value, \Illuminate\Routing\Route $route) {
            $dpia = app()->request->route('dpias');

A lot of my traits:

Traits/MorphsToManySecurityMeasures.php (in context of class App\Models\Org\System)

Access to an undefined property Illuminate\Database\Eloquent\Model::$id.
    public static function bootMorphsToManySecurityMeasures()
    {
        static::deleted(function(Model $model) {
            \DB::table('security_measurables')
                ->where('security_measurable_type', $model->getMorphClass())
                ->where('security_measurable_id', $model->id)
                ->delete();
        });
    }

Thank you for your _complete_ report!

To me these seem to be three slightly different issues.

@szepeviktor thanks for your _helpful_ comments!

The first problem can be solved by adding the following PHPDoc annotation to your resource class:

/** @extends JsonResource<User> */

as described here.

The second problem can be resolved by changing it to either

$dpia = request()->route('dpias');

or

$dpia = app('request')->route('dpias');

The last problem is expected behavior, because Model does not have an id by definition. You can either ignore it by adding e.g. // @phpstan-ignore-next-line, or you could create a custom Model class:

/** 
 * @property int $id
 */
class MyCustomModel extends Model {}

and use that as a typehint.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danielcosta picture danielcosta  路  4Comments

AJenbo picture AJenbo  路  4Comments

Gummibeer picture Gummibeer  路  3Comments

fgilio picture fgilio  路  4Comments

szepeviktor picture szepeviktor  路  4Comments