我有一个俱乐部表,其中有字段:
id,name,est_date,CEO ....
我的玩家表有字段:
id, name,DOB,height,weight, .....
player_history 表:
id,player_id,previous_club_id,current_club_id,position, ......
club_histories 表:
club_id, championship_id,status, ...
在club_histories的查看页面中,我可以显示:
club name,championship name and status...
但我也想在club_histories表中显示俱乐部各自的球员...这怎么可能??? 请回复...
编辑部分:添加代码
路由.php:
Route::resource('clubhistories','ClubHistoryController');
ClubHistoryController.php:
public function show($id,ClubHistory $clubhistories)
{
$club=$clubhistories->find($id);
$clubhistories=$clubhistories->getPlayerByCLub();
return view('pages.singleClub',compact('club','clubhistories'));
}
ClubHistoryRepository.php
public function getPlayerByCLub(){
return $this->makeModel() ->join('clubs', 'clubs.id', '=', 'club_histories.club_id')
->join('player_histories','player_histories.current_club_id', '=', 'clubs.id')
->join('players', 'players.id', '=', 'player_histories.player_id')
->get([
'clubs.name',
'player_histories.player_id',
'players.name',
]);
}
SingleClub.blade.php:
<div class="tab-content">
<div class="tab-pane in active" id="basic">
<table class="table table-condensed">
<thead>
<tr>
<th>Name</th>
<th>contact</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name:</td>
<td>{!!$club->club->name!!}</td>
</tr>
<tr>
<td>Contact:</td>
<td>{!!$club->club->contact_no!!}</td>
</tr>
</tbody>
</table>
</div>
<div class="tab-pane fade" id="players">
<table class="table table-condensed">
<thead>
<tr>
<th>Player name</th>
<th>Position</th>
</tr>
</thead>
<tbody>
@foreach($clubhistories as $clubhistories)
<tr>
<td> {!! $clubhistories->name!!} </td>
<td> {!! $clubhistories->position!!} </td>
</tr>
@endforeach
</tbody>
</table>
</div>
上面使用的连接和您给出的解决方案给出了相同的解决方案..我想将俱乐部对象中的俱乐部历史记录找到的相同ID传递给俱乐部历史对象,以便显示俱乐部的所有球员.. 你能帮我吗..
最佳答案
您可以通过联接(更少的查询)或 Eloquent 关系(看起来更简单的代码)查看 Club_histories 中的玩家。
使用Eloquent Relationships (假设您已设置它们):
$histories = ClubHistory::with('players', 'club')->get();
// In your blade view
@foreach ($histories as $history)
<p>{{ $history->club->name }} won {{ $history->championship_name }}</p>
<p>Status: {{ $history->status }}
<div>Player list:
<ul>
foreach ($history->players as $player)
<li>{{ $player->name }}</li>
@endforeach
</ul>
</div>
@endforeach
与查询生成器连接:
DB::table('club_histories')
->join('player_histories',
'player_histories.current_club_id', '=', 'club_histories.club_id')
->join('players', 'players.id', '=', 'player_histories.player_id')
->join('club', 'club.id', '=', 'club_histories.club_id')
->get([
'club.name AS clubName',
'club_histories.championship_name',
'club_histories.status',
'player_histories.player_id',
'players.name AS playerName',
]);
请注意,上面的代码将为每个 Club_history 每个玩家返回一行
编辑:
查看您的代码后,这是上面的一个版本,它应该适用于您的非标准代码库。请务必将 ID 传递给 getPlayerByClub($id)
。
ClubHistoryRepository.php
public function getPlayerByCLub($clubHistoryId) {
return $this->makeModel()
->join('clubs', 'clubs.id', '=', 'club_histories.club_id')
->join('player_histories', 'player_histories.current_club_id', '=', 'clubs.id')
->join('players', 'players.id', '=', 'player_histories.player_id')
->where('club_histories.id', '=', $clubHistoryId)
->get([
'clubs.id',
'club_histories.championship_name',
'club_histories.status',
'player_histories.player_id',
'clubs.name',
'players.name',
]);
}
关于php - 我想显示各个俱乐部的所有球员,但在 laravel 的表格中遇到一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31680111/