Larastan: [Nova] Access to model properties

Created on 24 Oct 2018  路  8Comments  路  Source: nunomaduro/larastan

  • Larastan Version: v0.3.4
  • --level used: 5

Description:

Larastan detects false positives in Nova resources.
For example: when I access $this->event, on an Audit resource, Nova actually accesses event on the Audit model.

Laravel code where the issue was found:

namespace App\Nova;

/**
 * Defines an audit for Nova.
 */
class Audit extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \OwenIt\Auditing\Models\Audit::class;

    /**
     * Get the value that should be displayed to represent the resource.
     *
     * @return string
     */
    public function title()
    {
        return $this->event . ' - ' . $this->auditable;
    }
}

Most helpful comment

We don't support custom getters yet. @alberto-bottarini you can create a new issue for that.

All 8 comments

Just ran into this when trying Larastan for the first time.

Since the main interface for this app is Laravel Nova, the majority of the issues reported by Larastan are actually this false-positive. At the moment, I suppose I'll need to exclude those files.

trying Larastan for the first time.

Very nice!

I'll need to exclude those files.

You may also contribute some code.

You can add @mixin <ModelClass> to the docblock for the Nova resource class.

It still does not work for me:

/**
 * @mixin \App\CourseEdition
 */
class CourseEdition extends Resource
{

    public function title()
    {
        return $this->courseTitle;
    }

courseTitle is a custom accessor that uses getCourseTitleAttribute on CourseEdition model:

    public function getCourseTitleAttribute()
    {
        return $this->course->title;
    }

That should work - yes. The mixing instruction should send the model to the pipes of Larastan no?

Maybe could custom accessor be the problem? When I will be back i will try with a standard property.

We don't support custom getters yet. @alberto-bottarini you can create a new issue for that.

For what it's worth, I had this same problem, and adding @mixin wasn't the solution here either since attributes and relations are all magical in Laravel.
But I noticed that the callbacks get two parameters, a NovaRequest and the model. An undocumented feature, I guess, but this would circumvent having to use $this in that context.

Example:

Text::make('title')
    ->showOnUpdating(function (NovaRequest $request, MyModel $model) {
        // avoid $this by using $model
        return $model->has_title;
    })
Was this page helpful?
0 / 5 - 0 ratings

Related issues

fico7489 picture fico7489  路  3Comments

bogdankharchenko picture bogdankharchenko  路  4Comments

grcasanova picture grcasanova  路  4Comments

LucianoVandi picture LucianoVandi  路  4Comments

mariomka picture mariomka  路  4Comments