Php Laravel视图显示:@foreach在@if内

Php Laravel视图显示:@foreach在@if内,php,laravel,laravel-5,laravel-5.6,Php,Laravel,Laravel 5,Laravel 5.6,我在我的Laravel视图中使用MaterialSecss CSS框架,并尝试设置布局,以便: 如果只有一篇文章可用,它将显示6列宽, 抵消3 如果只有2篇文章,它们将并排显示6栏宽 如果有两个以上的帖子,那么它们将显示为4列宽 我将下面的代码作为视图的一部分,但它不喜欢它,并返回以下内容: Parse error:syntax error,意外的'elseif'(T_elseif)(View:/home/testuser/sites/lsapp/resources/views/posts/

我在我的Laravel视图中使用MaterialSecss CSS框架,并尝试设置布局,以便:

  • 如果只有一篇文章可用,它将显示6列宽, 抵消3
  • 如果只有2篇文章,它们将并排显示6栏宽
  • 如果有两个以上的帖子,那么它们将显示为4列宽
我将下面的代码作为视图的一部分,但它不喜欢它,并返回以下内容:

Parse error:syntax error,意外的'elseif'(T_elseif)(View:/home/testuser/sites/lsapp/resources/views/posts/index.blade.php)

@if(计数($posts)>0)
@如果(计数($posts)==1)
@foreach($posts作为$post)
@elseif(计数($posts)==2)
@foreach($posts作为$post)
@否则
@foreach($posts作为$post)
@恩迪夫

由{{$post->user->name}创建
在{{{$post->created_at->toDateString()}}上{{{$post->created_at->toTimeString()}

@endforeach @否则 没有找到帖子

@恩迪夫
我想这和foreach有关,但我不确定


注意:也许有更好的方法可以做到这一点,但我在自学PHP之后正在学习Laravel,所以这不是世界末日。

你需要
@endforeach

@foreach($posts as $post)
....
@endforeach
错误表示出现意外的
@elseif
,因为解析器希望您在之前结束foreach。

您可以尝试以下操作:

   @if (count($posts) === 1)
       <?php $class = 'col s12 m6 offset-m3'; ?>
    @elseif (count($posts) === 2)
       <?php $class = 'col s12 m6'; ?>
    @else
        <?php $class = 'col s12 m4'; ?>
    @endif

    @foreach($posts as $post)
        <div class="{{ $class }}">
       //rest of code...

    @endforeach
@if(计数($posts)==1)
@elseif(计数($posts)==2)
@否则
@恩迪夫
@foreach($posts作为$post)
//代码的其余部分。。。
@endforeach

是,必须正确关闭每个指令。这意味着在每个
@foreach
语句之后都应该有结束标记
@endforeach

您遇到了问题,因为您没有正确关闭指令。试试这样的

@if(count($posts) > 0)
    <div class="row">
        @foreach($posts as $post)
            <div class="@if (count($posts) === 1) col s12 m6 offset-m3 @elseif (count($posts) === 2) col s12 m6 @else col s12 m4 @endif">


                <a href="posts/{{$post->id}}">
                    <div class="card hoverable">
                        <div class="card-image">
                            <img src="http://via.placeholder.com/460x230">
                            <span class="card-title">{{$post->title}}</span>
                            <a class="btn-floating halfway-fab waves-effect waves-light red hover"><i
                                        class="material-icons">remove_red_eye</i></a>
                        </div>
                        <div class="card-content">
                            <p>
                                Created by {{$post->user->name}}<br>
                                <small>on {{$post->created_at->toDateString()}}
                                    at {{$post->created_at->toTimeString()}}</small>
                            </p>
                        </div>
                    </div>
                </a>
            </div>
        @endforeach
    </div>
@else
    <div class="row">
        <div class="col s12">
            <div class="card">
                <p>No posts found</p>
            </div>
        </div>
    </div>
@endif
@if(计数($posts)>0)
@foreach($posts作为$post)

由{{$post->user->name}创建
在{{$post->created_at->toDateString()}上 在{{$post->created_at->toTimeString()}

@endforeach @否则 没有找到帖子

@恩迪夫
如果有困难。你可以试试

@php @endphp

我建议对照posts.count()进行检查,然后根据编号进行检查。包括带有所需COL的blade指令。因此,如果count==2,则包含('..')。有了它,你就有了一些可恢复的代码(指令)和消除循环。这很有效,谢谢!出于某种原因,我不认为只是将其添加到类中,而不是为每个场景添加一个单独的div。
@php @endphp