mardi 3 avril 2018

Laravel View display: @foreach within @if

I'm using the materialisecss CSS framework within my Laravel Views, and I'm trying to set my layout such that:

  • if there is only one post available, it is displayed 6 columns wide, offset by 3
  • if there are only 2 posts, they are both displayed 6 columns wide side by side
  • if there are more than 2 posts they are displayed 4 columns wide.

I have the code below as part of my view, but it doesn't like it, and returns the following:

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/">
                <div class="card hoverable">
                    <div class="card-image">
                        <img src="http://via.placeholder.com/460x230">
                        <span class="card-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 <br>
                            <small>on  at </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

I think it has something to do with the foreach, but I'm not certain.

Note: There is probably a far better way of doing this, but I'm currently learning Laravel after teaching myself PHP, so it's not the end of the world.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire