How to display username instead of id in a list in octobercms

875 Views Asked by At

In my case I have a backend list and I got the updated UserId on the list. But I have no Idea how to display the Username instead of the UserId. Is there anyone can help me to get out of this? See the list here

2

There are 2 best solutions below

1
On BEST ANSWER

Hi you have to use the relation field type, change your columns.yaml and instead of use user_id something like this:

user:
 label: Username
 type: relation
 select: login

And remember user is the name of your relationship defined in the belongsTo array

You should have defined the relationship right in your model

<?php

class YourModel extends Model
{
  $belongsTo = ['user'=>'Backend\Models\User'];
}

For further information http://octobercms.com/docs/backend/lists#column-relation

0
On

in your YourModel.php ,add this relation

public $hasOne = [
        'user' => ['Backend\Models\User', 'key' => 'id', 'otherKey' => 'user_id']
            ];

in your columns.yaml,add this

 user:
        label: 'Username'
        type: text
        select: username //field from users table
        relation: user //relation name is the key defined in model "hasone" relation