Laravel-admin: accessor doesn't work in grid

Created on 2 Apr 2017  路  11Comments  路  Source: z-song/laravel-admin

I have accessor in my model:

public function getFullNameAttribute()
    {
      return $this->name." ".$this->surname;
    }

When use

$grid->person()->full_name();

I see nothing :(

This way works:

$grid->person()->name();
$grid->person()->surname();

All 11 comments

I think you should learn not only How-to-use-Laravel-Framework but also How-to-use-PHP first.

@windyson2008 Could you just say what you mean? I use accessors in many project, everytime it just works, even here if I put code:

echo Persons::find(1)->full_name; - it works

I got Full Name as expected, my question is why it doesn;t work when I call it as

$grid->person()->full_name();

If You see any my mistake, just write it.

You have no full_name() function....

@edwinhuish I'ts not function, it's just column name...

I have no name() function nor surname() function, but it shows me correct data from table :) (it's just column name isn't it?)
full_name should be treated as column name (by accessor in model), isn't it?
I'm pretty sure it worked this way in an earlier version.

try this:

public function fullName()
    {
      return $this->name." ".$this->surname;
    }

And you can also try this: model-grid-column

$grid->column('full_name')->display(function () {
    return $this->name. ' ' . $this->surname;
});

@edwinhuish it will not work because Persons is related table... OK, maybe I was not very accurate, I will show how looks my controlers/models:

HardwareController.php:

// a lot of stuff before of course...
public function index()
    {
        return Admin::content(function (Content $content) {
            echo Persons::find(1)->full_name; // it shows me full_name correctly (so, accessor works!)
        });
    }

protected function grid()
    {
        return Admin::grid(Hardware::class, function (Grid $grid) {
            $grid->person()->name(); // works!
            $grid->person()->surname(); // works!
            $grid->person()->full_name(); // doesn't work
}

Hardware.php (model)

public function person()
    {
        return $this->belongsTo(Persons::class);
    }

Persons.php (model)
persons have colmns name and surname

public function hardware()
    {
        return $this->hasMany(Hardware::class);
    }

    public function getFullNameAttribute()
    {
      return $this->name." ".$this->surname;
    }

so, if I have accessor according to the documentation https://laravel.com/docs/5.4/eloquent-mutators#defining-an-accessor - why full_name column don't show me correct data?

Use $grid->column($field, $lable) your problem is lazy...

@edwinhuish I'm afraid You don't understand my problem :( $grid->column($field, $lable) will show me name+surname?

OK, I realised it by this code:

$grid->person_id('Osoba')->display(function($person_id) { return Persons::find($person_id)->name.' '.Persons::find($person_id)->surname; });

..but I'm pretty sure it worked with accessors before

@qcol your code will have a bad performance.
Accessor is not working here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abufalbo picture abufalbo  路  3Comments

cdhraesaemer picture cdhraesaemer  路  3Comments

evans-kim picture evans-kim  路  3Comments

benny-sun picture benny-sun  路  3Comments

fokoz picture fokoz  路  3Comments