Voyager: Show in Browser table not ID but Relationships name or title

Created on 15 Mar 2017  路  13Comments  路  Source: the-control-group/voyager

  • Laravel Version: 5.3
  • Voyager Version: 0.11
  • PHP Version: 5.6
  • Database Driver & Version: mySql 5.5

Description:

Is it possible to view in table not ID of Relationships data from dropdown select but Relationships name or title?
http://joxi.ru/LmGy3g8t6o77Al

Most helpful comment

I was able to solve this by adding a second parameter that declares the related foreign key to the cameCasedId method required by Voayger.

public function camelCasedMethodId()
{
    return $this->belongsTo('App\User', 'user_id');
}

see this thread: http://stackoverflow.com/questions/24469122/laravel-belongsto-not-working

Maybe this should be included in the docs?

All 13 comments

It looks like this feature has been added in #715. I went over the issues board but I could't figure out whether this works automatically or if we have to do something to get it to work.

I'm facing the same problem, it shows the id not the title.
From what I read in documentation, if you pass the slug page it "should" show the title and a link.
{ "relationship": { "key": "id", "label": "name", "page_slug": "admin/pages" } }
It keeps showing the id of the relation. Same goes for the link, it does not show.
Another issue I can't get to work are the items if the relation is oneToMany. If you set the BREAD to show this item in the browse view, if wont work... Not certain if it's a bug or what I'm doing wrong

I was able to solve this by adding a second parameter that declares the related foreign key to the cameCasedId method required by Voayger.

public function camelCasedMethodId()
{
    return $this->belongsTo('App\User', 'user_id');
}

see this thread: http://stackoverflow.com/questions/24469122/laravel-belongsto-not-working

Maybe this should be included in the docs?

+1 for @toddvalentine suggestion. The docs are correct except in this last part. The docs say:

return $this->belongsTo(User::class);

when they should be saying (following the example given in the docs):

return $this->belongsTo('App\User', 'author_id');

EDIT:

Forgot to say that the model 'AppUser' will only exist if you chose to create a model when you created the table, or created the model by hand.

@xotik, using User::class vs 'App\User' shouldn't matter. The real 'fix' would be the addition of the second parameter

@fletch3555 , you're probably right, haven't tested that, just made both changes at once, and it just worked :)

Soooo....after saying that it was working, today I clicked on the Add New button, and an error popped out.

After a lot of googling I found someone that was saying that the cause of the problem could be:

The problem was that there was an existing column called group, and the relationship method was also called group, so the table column value was taking precedence over the relationship method.
Renaming the relationship method, or the group column to something like group_id are both suitable solutions (I'd suggest the group_id route).

So I did that (renamed the group_id column), and now everything works as expected (I can create new items again) but the labels are gone, and I'm back with the ID's again.

I can see the labels when I麓m in edit or create mode, but not on browse or view modes.

Any ideas?

Nevermind my previous question, I was making it all wrong.

@xotik how did you fix it, i have the same issue?

@SerjoA His solution offered on March 31 works. You have to be sure that the relationship method name and the second parameter of the belongsTo call are the same - except the method name MUST be camel case.

Hi there,
adding a second parameter that declares the related foreign key to the cameCasedId method required by Voayger, not work for me in the BROWSE view.

I'm able to get relationship in READ and EDIT, but in BROWSE i get always the id instead the label.

This is my simple function

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

Any ideas?

@Errecci, the Browse view issue is unrelated to this one. Also, this one is old and already has an answer (not sure why it wasn't closed previously), so I'm going to close it. Please find the Browse issue or create your own.

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

TXRRNT picture TXRRNT  路  3Comments

duongsieu picture duongsieu  路  3Comments

vaggelis2018 picture vaggelis2018  路  3Comments

winex01 picture winex01  路  3Comments

wp-src picture wp-src  路  3Comments