【问题标题】:i want to display the value of table instead of displaying id in laravel我想显示表的值而不是在 laravel 中显示 id
【发布时间】:2015-10-11 20:19:25
【问题描述】:

我正在使用 laravel 5 进行项目。 我在使用不同 ID 的另一个表中使用同一表的同一列 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)

我在 playerhistory 模型的 eleqount 中有如下关系:

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

在两支球队之间的比赛中,假设阿根廷和巴西我们正在使用俱乐部表并存储双方的俱乐部名称,以及如何在视图页面中显示两个俱乐部名称????

请回复...

【问题讨论】:

  • 你必须 create relations in the eloquent model 为你的 $playerhistories
  • 我已经做了一个elewuent并将一个关系描述为: public function club() { return $this->belongsTo('App\Club'); }

标签: php mysql laravel migration project


【解决方案1】:

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

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

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

有了这些,您可以致电$playerhistories-&gt;currentClub-&gt;name$playerhistories-&gt;previousClub-&gt;name 从俱乐部获取数据。

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

【讨论】:

    猜你喜欢
    • 2019-03-16
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 2021-01-18
    相关资源
    最近更新 更多