2014-08-27 37 views
0

我有两个相关的表像这样获得/数据显示:Laravel:从相关表

出版商

| id | name  | 
|----|----------| 
| 1 | EA Games | 
| 2 | Nintendo | 

游戏

| id | publisher_id | title  | 
|----|--------------|-------------| 
| 1 | 1   | FIFA 2014 | 
| 2 | 2   | Super Mario | 
| 3 | 1   | The Sims | 

我的索引页是建立这样的:

public function index(){ 
    $games = Game::all(); 
    return View::make('/games/index', compact('games')); 
} 

当然,我的刀模板是这样的:

<table class="table table-striped"> 
    <thead> 
     <tr> 
      <th>Title</th> 
      <th>Publisher</th> 
      <th>Complete</th> 
      <th>Actions</th> 
     </tr> 
    </thead> 
    <tbody> 
     @foreach($games as $game) 
     <tr> 
      <td>{{ $game->title }}</td> 
      <td>{{ $game->publisher_id }}</td> 
      <td>{{ $game->complete }}</td> 
      <td> 
       <a href="{{ action('[email protected]', $game->id) }}" class="btn btn-default">Edit</a> 
       <a href="{{ action('[email protected]', $game->id)}}" class="btn btn-danger">Delete</a> 
      </td> 
     </tr> 
     @endforeach 
    </tbody> 
</table> 

在出版商的细胞,我想显示发布者名称(取自“出版商的数据库表),而不是出版商的标识

请,我怎么能做到这一点?

在此先感谢

回答

1

你需要设置你的模型关系,通过增加这个方法到您的游戏模式:

public function publisher() 
{ 
    return $this->belongsTo('Publisher'); 
} 

然后你就可以在你的视图中使用$游戏 - >个出版者>名称。

+0

谢谢,它的作品非常好! – Ivan 2014-08-27 10:08:22