Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel中使用@foreach时如何排除项目?_Php_Laravel_Blade_Laravel 5.3 - Fatal编程技术网

Php 在Laravel中使用@foreach时如何排除项目?

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

在Laravel中使用@foreach(blade)时如何排除项目

示例:
用户有一些文章,在文章详细信息页面:

<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在控制器甚至模型中处理数据,视图仅用于显示数据。