模型
/**
* Get the author
*/
public function author()
{
return $this->belongsTo('App\Models\Author');
}
/**
* Get the author books
*/
public function book()
{
return $this->belongsTo('App\Models\Book');
}
控制器
public function getAuthorBook(){
$authors = Author::with('book')->get();
return view('user.author', compact('authors');
}
查看
<table>
<thead>
<tr>
<th class="text-center">Author</th>
<th class="text-center">book tilte</th>
<th class="text-center">Description</th>
</tr>
</thead>
<tbody>
@foreach($authors as $auth)
<tr>
<td rowspan="{{count($auth->book)+1}}">
{{$auth->name}}
</td>
<td>
@foreach($auth->book as $book)
<tr>
{{$book->libelle}}
</tr>
@endforeach
<td>
<td>
@foreach($auth->book as $book)
<tr>
{{$book->description}}
</tr>
@endforeach
<td>
</tr>
@endforeach
</tbody>
</table>
我想用他们的书来展示作者,基本上是一列中的作者和另一列中的作者,以表示不同的行,如下图所示: table with vertical rowspan
请帮我实现这个动态表
答案 0 :(得分:0)
您必须为第一列添加条件才能添加rowspan&amp;保持一切类似于你保持简单行的方式。
另外,我假设{{count($auth->book)}}
将添加rowspan。但我不确定你为什么在计数中加1。
@foreach($authors as $auth)
@php ($first = true)
@foreach($auth->book as $book)
<tr>
@if($first == true)
<td rowspan="{{count($auth->book)}}">
{{$auth->name}}
<td>
@php ($first = false)
@endif
</td>
<td>
{{$book->libelle}}
</td>
<td>
{{$book->description}}
</td>
</tr>
@endforeach
@endforeach
我希望这会有所帮助。