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