我想显示 table 的值而不是在 laravel 中显示 id

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

我正在使用 laravel 5 进行项目。 我在另一个使用不同 ID 的 table 中使用相同 table 的同一列 2 次。 现在,当我想在视图页面中将两个名称分别显示到 id 时,它显示问题..

<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>

我已插入数据,插入成功.. 当我显示 id 时它只显示 id..

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

但问题是当我想显示以前的俱乐部名称和当前的俱乐部名称时...

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

它显​​示了这个问题: 试图获取 属性 的非对象(视图:/var/www/fcimsProject/resources/views/admin/playerhistories/index.blade.php)

我在 eleqount of playerhistory 模型中有一个关系:

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????

请回复...

您有一个不符合 laravel 标准的外键。在这种情况下,您必须创建关系并告诉 eloquent 外键是什么。

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

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

有了这些,您可以调用 $playerhistories->currentClub->name$playerhistories->previousClub->name 从俱乐部获取数据。

有关如何与自定义外键建立关系的信息可以在 the laravel documentations 中找到。