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

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

WBOY
Release: 2016-08-10 09:07:21
Original
1330 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

@endforeach
`

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template