Mysql Laravel在表中与rowspan和<;tr>;
型号Mysql Laravel在表中与rowspan和<;tr>;,mysql,laravel,eloquent,blade,Mysql,Laravel,Eloquent,Blade,型号 /** * 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 getAuth
/**
* 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>
作者
书柜
描述
@foreach($auth作为作者)
{{$auth->name}
@foreach($auth->book as$book)
{{$book->libelle}
@endforeach
@foreach($auth->book as$book)
{{$book->description}
@endforeach
@endforeach
我想展示作者及其书籍,基本上是一列中的作者,另一列中的作者的书籍,以在不同的行上表示,如下图所示:
请帮助我实现此动态表您只需为第一列添加条件即可添加行span&并保持与简单行类似的所有内容。 另外,我假设
{{count($auth->book)}}
将添加行跨度。但我不知道你为什么在计数中加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
@foreach($auth作为$auth的作者)
@php($first=true)
@foreach($auth->book as$book)
@如果($first==true)
{{$auth->name}
@php($first=false)
@恩迪夫
{{$book->libelle}
{{$book->description}
@endforeach
@endforeach
我希望这能有所帮助。你能详细说明一下问题吗?