【发布时间】:2021-09-22 02:17:25
【问题描述】:
我使用这样的刀片文件显示了一个名为 community_competition 的数据透视表:
但我想这样表现,怎么办?
| no | name | push up | sit up | back up | squat jump |
|---|---|---|---|---|---|
| 1. | Dr Keith Crist | 20 | 10 | 12 | 23 |
| 2. | Jaren Wunsch | 34 | 13 | 24 | 53 |
这是我用来展示桌子的刀片:
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">name</th>
<th scope="col">push up</th>
<th scope="col">sit up</th>
<th scope="col">back up</th>
<th scope="col">squat jump</th>
</tr>
</thead>
<tbody>
@php
$no = 1;
@endphp
@foreach ($data as $v)
<tr>
<th scope="row">{{ $no++ }}</th>
<td>{{ $v->community->name}}</td>
<td>{{ $v->point}}</td>
</tr>
@endforeach
</tbody>
CommunityCompetition模特:
public function community()
{
return $this->belongsTo(Community::class);
}
public function competition()
{
return $this->belongsTo(Competition::class);
}
CommunityCompetition控制器:
public function index()
{
$data = CommunityCompetition::with(['community', 'competition'])->get();
return view('cms.rank.index', compact('data'));
}
community表:
$table->id();
$table->string('name', 100);
| id | name |
|---|---|
| 1 | Dr.Keith Crist |
| 2 | Jaren Wunsch |
competition表:
$table->id();
$table->string('name', 100);
| id | name |
|---|---|
| 1 | push up |
| 2 | sit up |
| 3 | back up |
| 4 | squat jump |
community_competition表:
$table->id();
$table->bigInteger('community_id')->unsigned();
$table->bigInteger('competition_id')->unsigned();
$table->integer('point')->nullable();
$table->timestamps();
$table->foreign('community_id')->references('id')->on('communities');
$table->foreign('competition_id')->references('id')->on('competitions');
| id | community_id | competition_id | point |
|---|---|---|---|
| 1 | 1 | 1 | 20 |
| 2 | 1 | 2 | 10 |
| 3 | 1 | 3 | 12 |
| 4 | 1 | 4 | 23 |
| 5 | 2 | 1 | 34 |
| 6 | 2 | 2 | 13 |
| 7 | 2 | 3 | 24 |
| 8 | 2 | 4 | 53 |
【问题讨论】:
标签: laravel eloquent laravel-blade laravel-8