Home  >  Article  >  Backend Development  >  How to exclude an item when traversing using foreach in Laravel?

How to exclude an item when traversing using foreach in Laravel?

WBOY
WBOYOriginal
2016-08-10 09:07:211305browse

How to exclude an item when using foreach in Laravel?
For example,
A user has multiple articles. On the details page of an article, the details of this article are displayed. Under the article details, other articles of the user are displayed:

<code><p>文章详情:</p>
<h2>{{$article->title}}</h2>
<p>{{$article->content}}</p>

<h4>该用户其他文章:</h4>
@foreach ($articles as $article)
<p>{{$article->title}}</p>
@endforeach</code>

Question:
How to exclude the articles already displayed above during foreach traversal?

Reply content:

How to exclude an item when using foreach in Laravel?
For example,
A user has multiple articles. On the details page of an article, the details of this article are displayed. Under the article details, other articles of the user are displayed:

<code><p>文章详情:</p>
<h2>{{$article->title}}</h2>
<p>{{$article->content}}</p>

<h4>该用户其他文章:</h4>
@foreach ($articles as $article)
<p>{{$article->title}}</p>
@endforeach</code>

Question:
How to exclude the articles already displayed above during foreach traversal?

`
@foreach ($articles as $a)

<code>@if($a->id != $article->id)
    {{$a->title}}
@endif</code>

@endforeach
`

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn