Laravel View显示:@if中的@foreach

时间:2018-04-03 15:13:11

标签: php laravel laravel-5 laravel-5.6

我在Laravel Views中使用了materialisecss CSS框架,我试图设置我的布局:

  • 如果只有一个帖子可用,则显示6列宽, 偏移3
  • 如果只有2个帖子,则它们并排显示6列并列
  • 如果有超过2个帖子,则会显示4列宽。

我将以下代码作为我观点的一部分,但它并不喜欢它,并返回以下内容:

Parse error: syntax error, unexpected 'elseif' (T_ELSEIF) (View: /home/testuser/sites/lsapp/resources/views/posts/index.blade.php)

    @if(count($posts) > 0)

    <div class="row">

        @if (count($posts) === 1)

            @foreach($posts as $post)
            <div class="col s12 m6 offset-m3">

        @elseif (count($posts) === 2)

            @foreach($posts as $post)
            <div class="col s12 m6">

        @else
            @foreach($posts as $post)
            <div class="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

我认为它与foreach有关,但我不确定。

注意:这可能是一种更好的方法,但我现在正在自学PHP之后学习Laravel,所以它不是世界末日。

5 个答案:

答案 0 :(得分:1)

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

@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

答案 1 :(得分:0)

您需要@endforeach

@foreach($posts as $post)
....
@endforeach

错误是说有一个意外的@elseif,因为解析器希望你以前结束foreach。

答案 2 :(得分:0)

你可以试试这个:

   @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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

如果困难的话。你可以尝试

@php @endphp