laravel输出变量到模板,变量在模板中再分一次类可以吗?这样可以只查询一次数据库
laravel输出变量到模板,变量在模板中再分一次类可以吗?这样可以只查询一次数据库。
比如:
下面是一个用户的后台首页控制器,返回该用户发表的所有文章,像这样:
<code> public function index() { $user=\Auth::user(); $articles = $user->articles; return view('user.dashboard.index', compact('articles')); }</code>
下面的代码是在view中显示所有文章:
<code><div class="card"> <div class="card-header"> 所有文章 </div> @if ($articles!=null) <table class="table table-sm"> <thead> <tr> <th>标题</th> <th>发布时间</th> <th>发布状态</th> <th>操作</th> </tr> </thead> <tbody> @foreach ($articles as $article) <tr> <td>{{$article->title}}</td> <td>{{$article->created_at}}</td> <td>{{$article->status}}</td> <td> <a class="btn btn-primary btn-sm" href="#" role="button"><i class="fa fa-eye"></i>详情</a> <a class="btn btn-primary btn-sm" href="#" role="button"><i class="fa fa-edit"></i>编辑</a> </td> </tr> @endforeach </tbody> </table> @else <p>没有文章</p> @endif </div></code>
可是,这些文章分为两类:
一类是“已发布”,数据库字段status,值为“1”,
一类是“未发布”,数据库字段status,值为“0”,
问题:
现在需要这两类文章分开显示,已发布的显示在一个card中,未发布的显示在一个card中,可以在模板中直接分类吗?这样的话就不用查两次数据库。
回复内容:
laravel输出变量到模板,变量在模板中再分一次类可以吗?这样可以只查询一次数据库。
比如:
下面是一个用户的后台首页控制器,返回该用户发表的所有文章,像这样:
<code> public function index() { $user=\Auth::user(); $articles = $user->articles; return view('user.dashboard.index', compact('articles')); }</code>
下面的代码是在view中显示所有文章:
<code><div class="card"> <div class="card-header"> 所有文章 </div> @if ($articles!=null) <table class="table table-sm"> <thead> <tr> <th>标题</th> <th>发布时间</th> <th>发布状态</th> <th>操作</th> </tr> </thead> <tbody> @foreach ($articles as $article) <tr> <td>{{$article->title}}</td> <td>{{$article->created_at}}</td> <td>{{$article->status}}</td> <td> <a class="btn btn-primary btn-sm" href="#" role="button"><i class="fa fa-eye"></i>详情</a> <a class="btn btn-primary btn-sm" href="#" role="button"><i class="fa fa-edit"></i>编辑</a> </td> </tr> @endforeach </tbody> </table> @else <p>没有文章</p> @endif </div></code>
可是,这些文章分为两类:
一类是“已发布”,数据库字段status,值为“1”,
一类是“未发布”,数据库字段status,值为“0”,
问题:
现在需要这两类文章分开显示,已发布的显示在一个card中,未发布的显示在一个card中,可以在模板中直接分类吗?这样的话就不用查两次数据库。
只很容易,$articles 已经是 collections ,所以透过 where 即可过滤..
如下:
已发布
<code><div class="card"> <div class="card-header"> 已发布-所有文章 </div> @if ($articles!=null) <table class="table table-sm"> <thead> <tr> <th>标题</th> <th>发布时间</th> <th>发布状态</th> <th>操作</th> </tr> </thead> <tbody> @foreach ($articles->where('status', 1) as $article) <tr> <td>{{$article->title}}</td> <td>{{$article->created_at}}</td> <td>{{$article->status}}</td> <td> <a class="btn btn-primary btn-sm" href="#" role="button"><i class="fa fa-eye"></i>详情</a> <a class="btn btn-primary btn-sm" href="#" role="button"><i class="fa fa-edit"></i>编辑</a> </td> </tr> @endforeach </tbody> </table> @else <p>没有已发布文章</p> @endif </div></code>
未发布
<code><div class="card"> <div class="card-header"> 未发布-所有文章 </div> @if ($articles!=null) <table class="table table-sm"> <thead> <tr> <th>标题</th> <th>发布时间</th> <th>发布状态</th> <th>操作</th> </tr> </thead> <tbody> @foreach ($articles->where('status', 0) as $article) <tr> <td>{{$article->title}}</td> <td>{{$article->created_at}}</td> <td>{{$article->status}}</td> <td> <a class="btn btn-primary btn-sm" href="#" role="button"><i class="fa fa-eye"></i>详情</a> <a class="btn btn-primary btn-sm" href="#" role="button"><i class="fa fa-edit"></i>编辑</a> </td> </tr> @endforeach </tbody> </table> @else <p>没有未发布文章</p> @endif </div></code>
可以在模板里面进行两次循环,每次使用if判断,Card A中只有状态是已发布才显示。
$articles = $user->articles;
才是查数据库的操作,模版中foreach只是对这个数据进行循环,不会查数据库

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP用於構建動態網站,其核心功能包括:1.生成動態內容,通過與數據庫對接實時生成網頁;2.處理用戶交互和表單提交,驗證輸入並響應操作;3.管理會話和用戶認證,提供個性化體驗;4.優化性能和遵循最佳實踐,提升網站效率和安全性。

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP的核心優勢包括易於學習、強大的web開發支持、豐富的庫和框架、高性能和可擴展性、跨平台兼容性以及成本效益高。 1)易於學習和使用,適合初學者;2)與web服務器集成好,支持多種數據庫;3)擁有如Laravel等強大框架;4)通過優化可實現高性能;5)支持多種操作系統;6)開源,降低開發成本。

PHP在數據庫操作和服務器端邏輯處理中使用MySQLi和PDO擴展進行數據庫交互,並通過會話管理等功能處理服務器端邏輯。 1)使用MySQLi或PDO連接數據庫,執行SQL查詢。 2)通過會話管理等功能處理HTTP請求和用戶狀態。 3)使用事務確保數據庫操作的原子性。 4)防止SQL注入,使用異常處理和關閉連接來調試。 5)通過索引和緩存優化性能,編寫可讀性高的代碼並進行錯誤處理。
