sabin maharjan
sabin maharjan

Reputation: 419

i want to display the value of table instead of displaying id in laravel

I am using laravel 5 for project. i am using same column of same table 2 times in another table using different id. now when i want to display both name respectively to id in view page it shows problem..

<div class="tab-pane fade in active" id="tab1primary">
    <ul>
        <label>PREVIOUS CLUB</label>
           {!! Form::select('previous_club_id',$club,null,array('class'=>'form-control')) !!}
              </ul>
 </div>
 <div class="tab-pane fade" id="tab2primary">
      <ul>
           <label>CURRENT  CLUB</label>
                 {!! Form::select('current_club_id',$club,null,array('class'=>'form-control')) !!}
      </ul>
 </div>

i have inserted data and inserted successfully.. and when i display id it shows id only..

 <td>{!!$playerhistories->current_club_id!!}</td> 
 <td>{!!$playerhistories->previous_club_id!!}</td> 

but problem is when i want to display previous club name and current club name...

<td>{!!$playerhistories->club->name!!}</td> 

it shows this problem: Trying to get property of non-object (View: /var/www/fcimsProject/resources/views/admin/playerhistories/index.blade.php)

i have a relationship in eleqount of playerhistory model as:

public function club()
{
    return $this->belongsTo('App\Club');
}

in an match event between two team,suppose argentina and brazil we are using club table and store club name of both and how to display in view page both club name????

please reply...

Upvotes: 2

Views: 1674

Answers (1)

Jerodev
Jerodev

Reputation: 33196

You have a foreign key that does not follow the laravel standards. In this case you have to create the relation and tell eloquent what the foreign key is.

public function currentClub() {
    return $this->belongsTo('Club', 'current_club_id');
}

public function previousClub() {
    return $this->belongsTo('Club', 'previous_club_id');
}

With these, you can call $playerhistories->currentClub->name and $playerhistories->previousClub->name to get the data from the clubs.

The info on how to make relations with custom foreign keys can be found in the laravel documentations.

Upvotes: 1

Related Questions