Laravel视图中交替展示数据库两表数据的实现需求
问题描述
我数据库中有两张表:
standard表
+----+---------+--------------+----------------------+------------------------------------+ | id | user_id | category | title | content | +----+---------+--------------+----------------------+------------------------------------+ | 1 | 1 | Minus | Architecto aperiam | Architecto aperiam odio mollitia. | | 2 | 1 | Onmnis | Est aspernatur | Est aspernatur. | | 3 | 1 | Dolor | Delectus ratione | Delectus ratione. | | 4 | 1 | Aests | Fugit occaecati aut | Fugit occaecati aut. |
quote表
+----+---------+------------------------------------------+------------------+ | id | user_id | content | author | +----+---------+------------------------------------------+------------------+ | 1 | 1 | Architecto aperiam odio mollitia | Author | | 2 | 1 | Evitar pecados na frente dos outros | Author |
我的HomeController代码:
class HomeController extends Controller { public function index() { return view('home', [ 'standard' => Standard::limit(5)->orderBy('id', 'desc')->get(), 'quote' => Quote::limit(3)->orderBy('id', 'desc')->get() ]); } }
home视图代码:
@foreach($standard as $standardPost) <div> <p>{{ ($standardPost->content) }}</p> </div> @endforeach @foreach($quote as $quotePost) <div> <p>{{ ($quotePost->content) }}</p> </div> @endforeach
当前首页展示效果:
Last row of the "standard" table Penultimate row of the "standard" table Antepenultimate row of the "standard" table ... Last row of "quote" table Penultimate row of "quote" table Antepenultimate row of "quote" table
我希望实现的交替展示效果:
Last row of the "standard" table Last row of "quote" table Penultimate row of the "standard" table Penultimate row of "quote" table Antepenultimate row of the "standard" table Antepenultimate row of "quote" table
解决方案
有两种方法可以实现这种交替展示的效果:
方法一:直接在视图中循环处理
不需要改动控制器,直接在视图里通过索引遍历两组数据,实现交替展示:
@php // 取两组数据中数量多的那个作为循环次数 $maxCount = max($standard->count(), $quote->count()); @endphp @for($i = 0; $i < $maxCount; $i++) {{-- 输出第i个standard内容 --}} @if(isset($standard[$i])) <div> <p>{{ $standard[$i]->content }}</p> </div> @endif {{-- 输出第i个quote内容 --}} @if(isset($quote[$i])) <div> <p>{{ $quote[$i]->content }}</p> </div> @endif {{-- 每组之间添加空白分隔,最后一组不加 --}} @if($i < $maxCount - 1) <br><br> @endif @endfor
方法二:在控制器中预先合并数据
修改控制器代码,把两个表的数据按索引配对,生成一个包含交替内容的集合,再传给视图:
class HomeController extends Controller { public function index() { $standards = Standard::limit(5)->orderBy('id', 'desc')->get(); $quotes = Quote::limit(3)->orderBy('id', 'desc')->get(); $maxCount = max($standards->count(), $quotes->count()); $combinedData = []; for ($i = 0; $i < $maxCount; $i++) { // 添加对应索引的standard内容 if (isset($standards[$i])) { $combinedData[] = ['type' => 'standard', 'content' => $standards[$i]->content]; } // 添加对应索引的quote内容 if (isset($quotes[$i])) { $combinedData[] = ['type' => 'quote', 'content' => $quotes[$i]->content]; } // 添加分隔标记,用于视图里的空白间隔 $combinedData[] = ['type' => 'separator']; } return view('home', ['combinedData' => $combinedData]); } }
然后修改视图,循环处理合并后的集合:
@foreach($combinedData as $item) @if($item['type'] !== 'separator') <div> <p>{{ $item['content'] }}</p> </div> @else <br><br> @endif @endforeach
两种方法都能实现你要的交替展示效果,方法二更直接,不需要修改控制器逻辑;方法一则把数据处理逻辑放在了控制器里,视图更简洁。
内容的提问来源于stack exchange,提问作者Itamar Campos
相关产品推荐
相关产品推荐

