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只是对这个数据进行循环,不会查数据库

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.
