Laravel-admin: How to access accessor with Model-Grid

Created on 2 Jan 2017  ·  7Comments  ·  Source: z-song/laravel-admin

I trying to get accessor from user model in the model-grid. but it not work.

The code below is it full name accessor from user model.

public function getFullNameAttribute() {
  return $this->name . ' ' . $this->last_name;
}

And I trying to access it in grid model. it not work.

$grid->column('full_name'); // fill_name is it accessor

and closure display() method

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

Most helpful comment

In your way, you should add the accessor to $appends property of model.

//in model
protected $appends = ['fullName'];

public function getFullNameAttribute()
{
  return $this->name . ' ' . $this->last_name;
}

// and in grid
$grid->column('fullName');

Or use the simplest way without define a accessor:

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

All 7 comments

In your way, you should add the accessor to $appends property of model.

//in model
protected $appends = ['fullName'];

public function getFullNameAttribute()
{
  return $this->name . ' ' . $this->last_name;
}

// and in grid
$grid->column('fullName');

Or use the simplest way without define a accessor:

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

Thank.

I have one more problem with relation input in Model-Form.

I have a user_bio column in User_meta table is relation with User table.

// User Model
public function user_metas() {
   return $this->hasOne('App\Models\UserMeta');
} 

I want to create input text field name bio and save it to User_meta when User it saved.

protected function form()
    {
        return Admin::form(User::class, function (Form $form) {

            $form->text('name')->rules('required');
            $form->text('last_name')->rules('required');
            $form->email('email')->rules('required');

            // How do it work.
            $form->text('user_metas.bio')->rules('required');

        });
    }

Thank.

@Natdanai-Wongtib try

$form->text('user_metas.user_bio')->rules('required');

Thank.

That Work. Sorry I'm was wrong.

Hi. I have a problem when relation save.

Cant' find id of relation table. in the user_meta table i have a column umeta_id instead of id

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update user_metas set bio = Caterpillar. Here was another moment it was good. aff, phone = 98085988435, updated_at = 2017-01-02 15:39:36 where id is null)

Define your relation use following code

public function user_metas()
 {
   return $this->hasOne('App\Models\UserMeta', 'umeta_id');
} 

Thank you very much.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joernroeder picture joernroeder  ·  3Comments

greentornado picture greentornado  ·  3Comments

clock1129 picture clock1129  ·  3Comments

benny-sun picture benny-sun  ·  3Comments

piian picture piian  ·  3Comments