Home > Article > Backend Development > How to exclude an item when traversing using foreach in Laravel?
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?
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
`