Voyager: How i get name on browse a field with relationship?

Created on 7 Jun 2017  路  4Comments  路  Source: the-control-group/voyager

How do I see the name of a related field instead of id?

question

Most helpful comment

So many posts asking for the same, and so many responses to the same post....
You need to add the field to the relation. Ex:


public function delegation()
{
 return $this->belongsTo('App\Delegacao', 'id');
}

or

public function userId()
{
  return $this->belongsTo(User::class, 'id');
}

If your relation is

{
    "relationship": {
        "key": "id",
        "label": "name",
        "page_slug": "admin/users"
    }
}

Will return the name of the user, and if you click it will go the the user details page

All 4 comments

So many posts asking for the same, and so many responses to the same post....
You need to add the field to the relation. Ex:


public function delegation()
{
 return $this->belongsTo('App\Delegacao', 'id');
}

or

public function userId()
{
  return $this->belongsTo(User::class, 'id');
}

If your relation is

{
    "relationship": {
        "key": "id",
        "label": "name",
        "page_slug": "admin/users"
    }
}

Will return the name of the user, and if you click it will go the the user details page

a little correction on the previous reply, because i was getting wrong relationship

public function userId()
{
return $this->belongsTo(User::class, 'foreign_key');
}

in the case of user it should be user_id

The above 2 comments are correct.

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ferrywae picture ferrywae  路  4Comments

Nagendra1421 picture Nagendra1421  路  3Comments

zzpwestlife picture zzpwestlife  路  3Comments

TXRRNT picture TXRRNT  路  3Comments

wislem picture wislem  路  3Comments