Laravel View display: #foreach within #if - php

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/{{$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
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.

You're getting the problem because you're not closing off the directives properly. Try something like this.
#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

You need to #endforeach
#foreach($posts as $post)
....
#endforeach
The error is saying that there is an unexpected #elseif because the parser expects you to end the foreach before.

you can try this:
#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

Yes every directive must be closed properly. Meaning you should have the closing tag #endforeach after every #foreach statement.

If it's difficult. You can try
#php #endphp

Related

Add class only on first carousel-cell (flickity)

i would have the class "opacity" inserted only at the first "carousel-cell".
For the rest in the loop, they should not be there.
<div class="main-carousel">
#foreach($projects as $p)
#if($p->getMedia('teaser')->first() ?? "")
<div class="carousel-cell">
<div class="row">
<div id="project-info" class="col-lg-3 text-md-right pr-md-5 project-info">
<div class="project opacity">
<h2 class="project-title mb-0">{{$p->name}}</h2>
<h3 class="project-category">Foto / Video Produktion</h3>
</div>
</div>
<div class="col-lg-9 project-img opacity">
<a href="{{url('projects')}}/{{$p->slug}}">
<div class="start-teaser">
<img src="{{asset('storage')}}/{{$p->getMedia('teaser')->first()->id}}/{{$p->getMedia('teaser')->first()->file_name}}" alt="Land Rover">
</div>
</a>
</div>
</div>
</div>
#endif
#endforeach
</div>
You could use the $loop->first variable to add a class only on the first iteration of the loop.
Like this: <div class="carousel-cell {{ $loop->first ? 'opacity' : '' }}">
See more here:
https://laravel.com/docs/9.x/blade#the-loop-variable

Problem with generating one extra slide using PHP FOR loop in Laravel Blade

I have a strange problem in Blade. I think is something obvious but I'm just stuck at it.
I have a Bootstrap carousel with cards in Larvel Blade view. I'm creating new "carousel-item" (slides) using if in for loop, but I'm facing the problem that it generates one empty slide. I cant figure it out why this is happening.
The following code should generate 2 slides with 4 cards in it. Unfortunately it creates 3 slides the last of them empty.
This is my code:
<div id="carousel{!! $type !!}" class="carousel slide" data-ride="carousel" data-touch="true">
<div class="carousel-inner">
<div class="carousel-item active">
<div class="row justify-content-center latest__properties_row">
#for($i=0;$i<=7;$i++)
#if(isset($latest[$type][$i]))
<div class="col-md-3 mb-3 latest__properties">
<div class="card">
#if(isset($latest[$type][$i]['photos']))
#if(isset($latest[$type][$i]['photos'][0]))
<a href="/properties/{!! $latest[$type][$i]['slug'] !!}"><img
class="img-fluid latest__properties__photo"
alt="{!! $latest[$type][$i]['title'] !!}"
src="/images/properties/{!! $latest[$type][$i]['photos'][0]['url'] !!}"></a>
#endif
#endif
<div class="card-body">
<div class="title">
<a href="/properties/{!! $latest[$type][$i]['slug'] !!}"><h4
class="card-title">{!! $latest[$type][$i]['title'] !!}</h4>
</a>
</div>
<!-- Slide like buttom -->
<button class="slide__like-btn">
<i class="far fa-heart"></i>
</button>
<!-- Slide badge -->
#if(isset($latest[$type][$i]['terms']['property_labels']))
#for($k=0;$k<=count($latest[$type][$i]['terms']['property_labels']);$k++)
#if(isset($latest[$type][$i]['terms']['property_labels'][$k]))
<div class="slide__badge {!! ($latest[$type][$i]['terms']['property_labels'][$k]['attributes'] != '' || $latest[$type][$i]['terms']['property_labels'][$k]['attributes'] != null) ? $latest[$type][$i]['terms']['property_labels'][$k]['attributes'] : '' !!} text-uppercase">
{!! $latest[$type][$i]['terms']['property_labels'][$k]['name'] !!}
</div>
#endif
#endfor
#endif
<ul>
#if(isset($latest[$type][$i]['rooms']) && $latest[$type][$i]['rooms'] != 0)
<li><b>Стаи:</b> {!! $latest[$type][$i]['rooms'] !!}</li>
#endif
#if(isset($latest[$type][$i]['bathrooms']) && $latest[$type][$i]['bathrooms'] != 0)
<li><b>Бани:</b> {!! $latest[$type][$i]['bathrooms'] !!}
</li>
#endif
#if(isset($latest[$type][$i]['sqm']) && $latest[$type][$i]['sqm'] != 0)
<li><b>Квадратура:</b> {!! $latest[$type][$i]['sqm'] !!} m²</li>
#endif
</ul>
<p class="latest__properties__id">
ID: {!! $latest[$type][$i]['unique_code'] !!}</p>
<p class="latest__properties__price">
<span class="euro">€ {!! $latest[$type][$i]['price'] !!}</span>
<span
class="bgn">{!! $latest[$type][$i]['price_bgn'] !!} лв.</span>
</p>
<p class="latest__properties__created__at">
{!! $latest[$type][$i]['created_at'] !!}
</p>
</div>
</div>
</div>
#if($i == 3)
</div>
</div>
<div class="carousel-item">
<div class="row justify-content-center latest__properties_row">
#endif
#endif
#endfor
</div>
</div>
<div class="carousel-item">
<div class="row justify-content-center latest__properties_row">
</div>
</div>
</div>
</div>
I just figured it out... I was looking like 1000 times the code and that I realize I just forgot an empty slide right after the for loop.

Divide by 4 for gallery

sorry for asking here but I can't seem to get my head around what I need to do to complete this.
I have a gallery that is pulled from the database and i'd like it so every division of 4 it adds a new row.
Here is my website: https://creativehedgehog.co.uk/gallery
i.e
<div class="row">
col3
col3
col3
col3
</div>
Is this correct? Here is my code
<div class="gallery-container">
<div class="gallery cf">
#php
$i=0;
#endphp
#foreach($galleries as $gallery)
#if($i % 4 == 0)
{{$i}}
<div class="row">
#endif
<div class="col-md-3">
<a href="{{$gallery->link}}" data-lightbox="image-1" data-title="{{$gallery->title}}">
<img src="{{$gallery->link}}" alt="{{$gallery->title}}">
</a>
</div>
#if($i % 4 == 0)
{{$i}}
</div>
#endif
#php
$i++;
#endphp
#endforeach
</div>
</div>
Use array_chunk function. array_chunk
<div class="gallery-container">
<div class="gallery cf">
#foreach(array_chunk($galleries,4,true) as $chank)
<div class="row">
#foreach($chank as $gallery)
<div class="col-md-3">
<a href="{{$gallery->link}}" data-lightbox="image-1" data-title="{{$gallery->title}}">
<img src="{{$gallery->link}}" alt="{{$gallery->title}}">
</a>
</div>
#endforeach
</div>
#endforeach;
</div>
</div>
You are adding a row element every fourth iteration, not placing 4 iteration into each row.
Look below:
<div class="gallery-container">
<div class="gallery cf">
#php
$i=0;
#endphp
#foreach($galleries as $gallery)
#if($i % 4 == 0)
#if($i != 0)
</div>
#endif
{{$i}}
<div class="row">
#endif
<div class="col-md-3">
<a href="{{$gallery->link}}" data-lightbox="image-1" data-title="{{$gallery->title}}">
<img src="{{$gallery->link}}" alt="{{$gallery->title}}">
</a>
</div>
#php
$i++;
#endphp
#endforeach
</div><!-- last row end -->
</div>
</div>
Try this and let the bootstrap flexbox do their job
<div class="gallery-container">
<div class="gallery cf">
<div class="container-fluid">
<div class="row">
#foreach($galleries as $gallery)
<div class="col-md-3">
<a href="{{$gallery->link}}" data-lightbox="image-1" data-title="{{$gallery->title}}">
<img src="{{$gallery->link}}" alt="{{$gallery->title}}">
</a>
</div>
#endforeach
</div>
</div>
</div>
</div>

Blade Template Layout Structure

I need to create a row for each 3 attachments. Something like
#foreach($email->attachment as $attach)
<div class="row">
<div class="col-lg-4"></div>
<div class="col-lg-4"></div>
<div class="col-lg-4"></div>
</div>
#endforeach
<div class="row">
<div class="col-lg-4">1st attachment</div>
<div class="col-lg-4">2nd attachment</div>
<div class="col-lg-4">3rd attachment</div>
</div>
<div class="row">
<div class="col-lg-4">4th attachment</div>
<div class="col-lg-4">5th attachment</div>
<div class="col-lg-4">6th attachment</div>
</div>
and so on! I had searched for the question but just found This link but It's actually different from my perspective.
Thanks
Use array_chunk - it is designed for this each purpose - so you can cut an array into sub-sizes and loop through each group
#foreach (array_chunk($email->attachment->toArray(), 3, true) as $array)
<div class="row">
#foreach($array as $attachment)
<div class="col-lg-4">{{ $attachment['id'] }}</div>
#endforeach
</div>
#endforeach
And using #Lukasgeiter's comment - you could also do it this way if it is a Laravel Collection
#foreach ($email->attachment->chunk(3) as $array)
<div class="row">
#foreach($array as $attachment)
<div class="col-lg-4">{{ $attachment->id }}</div>
#endforeach
</div>
#endforeach
Try this, it's faster than generate 2 arrays:
<div class="row">
#foreach( $email->attachment as $index => $attach)
<div class="col-lg-4">{{$attach}}</div>
#if( $index+1 === 0)
</div><div class="row">
#endif
#endforeach
</div>

how to do laravel with #foreach and #if

I have seen as "there are no posts" if they are available as comment even the URL is working property.
I have discover on laravel.com there is not lack of information or example inside the #if and #else . http://laravel.com/docs/4.2/templates#other-blade-control-structures.
here is my code is
#if ($post->comments)
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#endif
#foreach ($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#endforeach
however I have tried change as #if ($post->comments as $comment) is said "syntax error, unexpected 'as' (T_AS)" do you have any idea to solve it?
or you could use the blade's #forelse loop this way
#forelse($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#empty
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#endforelse
Just a guess, but I think this is what you wanted to do
#if ($post->comments)
#foreach ($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#endforeach
#else
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#endif
You are trying to put a foreach in the condition of an if statement
Put the foreach in the entire #if, and check for NOT comments :
#if (!$post->comments)
<div class="text-center jumbotron">
<h1 style="color:red;">There are no posts</h1>
</div>
#else
#foreach ($post->comments as $comment)
<div class="row">
<div class="col-xs-12">
{{{ $comment->user->name }}}:
<blockquote>{{{ $comment->text}}}</blockquote>
</div>
</div>
#endforeach
#endif
This should be the control flow you are looking for.

Categories