Php 在Laravel中使用@foreach时如何排除项目?
在Laravel中使用@foreach(blade)时如何排除项目 示例:Php 在Laravel中使用@foreach时如何排除项目?,php,laravel,blade,laravel-5.3,Php,Laravel,Blade,Laravel 5.3,在Laravel中使用@foreach(blade)时如何排除项目 示例: 用户有一些文章,在文章详细信息页面: <p>Article detail:</p> <h2>{{$article->title}}</h2> <p>{{$article->content}}</p> <h4>The other articles of this user:</h4> @foreach ($arti
用户有一些文章,在文章详细信息页面:
<p>Article detail:</p>
<h2>{{$article->title}}</h2>
<p>{{$article->content}}</p>
<h4>The other articles of this user:</h4>
@foreach ($articles as $article)
<p>{{$article->title}}</p>
@endforeach
文章详细信息:
{{$article->title}
{{$article->content}
此用户的其他文章:
@foreach($articles作为$article)
{{$article->title}
@endforeach
问题:在
@foreach
中,如何排除上面显示的文章?如果您不想在中的详细信息页显示该用户的其他文章:
在控制器上,您应发送此详细信息页面上的文章id以查看
在视野中
<p>Article detail:</p>
<h2>{{$article->title}}</h2>
<p>{{$article->content}}</p>
<h4>The other articles of this user:</h4>
@foreach ($articles as $article)
@if($article->id !== $article_id)
<p>{{$article->title}}</p>
@endif
@endforeach
文章详细信息:
{{$article->title}
{{$article->content}
此用户的其他文章:
@foreach($articles作为$article)
@如果($article->id!==$article\u id)
{{$article->title}
@恩迪夫
@endforeach
如果您不想在
中的详细信息页显示文章,则使用文章id
是变量
从控制器
发送:
在控制器上,您应发送此详细信息页面上的文章id以查看
在视野中
<p>Article detail:</p>
<h2>{{$article->title}}</h2>
<p>{{$article->content}}</p>
<h4>The other articles of this user:</h4>
@foreach ($articles as $article)
@if($article->id !== $article_id)
<p>{{$article->title}}</p>
@endif
@endforeach
文章详细信息:
{{$article->title}
{{$article->content}
此用户的其他文章:
@foreach($articles作为$article)
@如果($article->id!==$article\u id)
{{$article->title}
@恩迪夫
@endforeach
使用文章id
是变量
您可以从控制器
发送,有几种方法可以做到这一点。
一个选项是在模板中进行简单的if检入:
<p>Article detail:</p>
<h2>{{$article->title}}</h2>
<p>{{$article->content}}</p>
<h4>The other articles of this user:</h4>
@foreach ($articles as $otherArticle)
@if($article->id !== $otherArticle->id)
<p>{{$article->title}}</p>
@endif
@endforeach
有几种方法可以做到这一点。
一个选项是在模板中进行简单的if检入:
<p>Article detail:</p>
<h2>{{$article->title}}</h2>
<p>{{$article->content}}</p>
<h4>The other articles of this user:</h4>
@foreach ($articles as $otherArticle)
@if($article->id !== $otherArticle->id)
<p>{{$article->title}}</p>
@endif
@endforeach
从第二个元素开始使用for循环
<p>Article detail:</p>
<h2>{{$article->title}}</h2>
<p>{{$article->content}}</p>
@if (count($articles) > 1)
<h4>The other articles of this user:</h4>
@for ($i = 1; $i < count($articles); $i++)
<p>{{$articles[$i]->title}}</p>
@endfor
@endif
文章详细信息:
{{$article->title}
{{$article->content}
@如果(计数($articles)>1)
此用户的其他文章:
@对于($i=1;$ititle}
@结束
@恩迪夫
使用从第二个元素开始的for循环
<p>Article detail:</p>
<h2>{{$article->title}}</h2>
<p>{{$article->content}}</p>
@if (count($articles) > 1)
<h4>The other articles of this user:</h4>
@for ($i = 1; $i < count($articles); $i++)
<p>{{$articles[$i]->title}}</p>
@endfor
@endif
文章详细信息:
{{$article->title}
{{$article->content}
@如果(计数($articles)>1)
此用户的其他文章:
@对于($i=1;$ititle}
@结束
@恩迪夫
Artisans在控制器甚至模型中处理数据,视图仅用于显示数据。Artisans在控制器甚至模型中处理数据,视图仅用于显示数据。