Laravel the output variable to the template, can the variable be divided again in the template class? This allows you to query the database only once

Source: Internet
Author: User
Keywords Php laravel
Laravel the output variable to the template, can the variable be divided again in the template class? This allows you to query the database only once.
Like what:
Below is a user's background home controller, which returns all articles published by that user, like this:

    public function index()    {        $user=\Auth::user();        $articles = $user->articles;                return view('user.dashboard.index',  compact('articles'));    }

The following code shows all the articles in view:

            所有文章        @if ($articles!=null)    
  
   
   
      @foreach ($articles as $article) 
      @endforeach 
    
标题 发布时间 发布状态 操作
{{$article->title}} {{$article->created_at}} {{$article->status}} 详情 编辑
@else

没有文章

@endif

However, these articles fall into two categories:
The category is "published", the Database field status, the value is "1",
The category is "unpublished", the database field status, the value is "0",

Problem:
Now you need these two kinds of articles to display separately, the published display in a card, unpublished display in a card, can be directly categorized in the template? In this case, you don't have to check the database two times.

Reply content:

Laravel the output variable to the template, can the variable be divided again in the template class? This allows you to query the database only once.
Like what:
Below is a user's background home controller, which returns all articles published by that user, like this:

    public function index()    {        $user=\Auth::user();        $articles = $user->articles;                return view('user.dashboard.index',  compact('articles'));    }

The following code shows all the articles in view:

            所有文章        @if ($articles!=null)    
  
   
   
      @foreach ($articles as $article) 
      @endforeach 
    
标题 发布时间 发布状态 操作
{{$article->title}} {{$article->created_at}} {{$article->status}} 详情 编辑
@else

没有文章

@endif

However, these articles fall into two categories:
The category is "published", the Database field status, the value is "1",
The category is "unpublished", the database field status, the value is "0",

Problem:
Now you need these two kinds of articles to display separately, the published display in a card, unpublished display in a card, can be directly categorized in the template? In this case, you don't have to check the database two times.

It's easy, $articles is collections, so you can filter through where.
As follows:

has been published

            已发布-所有文章        @if ($articles!=null)    
  
   
   
      @foreach ($articles->where('status', 1) as $article) 
      @endforeach 
    
标题 发布时间 发布状态 操作
{{$article->title}} {{$article->created_at}} {{$article->status}} 详情 编辑
@else

没有已发布文章

@endif

Not published

            未发布-所有文章        @if ($articles!=null)    
  
   
   
      @foreach ($articles->where('status', 0) as $article) 
      @endforeach 
    
标题 发布时间 发布状态 操作
{{$article->title}} {{$article->created_at}} {{$article->status}} 详情 编辑
@else

没有未发布文章

@endif

You can make two loops within the template, and each time you use the If judgment, only the status is published in Card A.

$articles = $user->articles;is to check the operation of the database, the template is only the foreach data loop, do not check the database

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.