I am using blade for the first time, and i need to change the styling of this div element depending on how many properties are available. I need to write an if statement which hides the div if equal or less than one and add a different class if equal to 2.
how would i write this using blade or php?
#if (isset($participatingProperties) && !empty($participatingProperties) && is_array($participatingProperties))
<?php $i = 0; ?>
#foreach ($participatingProperties as $key => $property)
#if ($i++ % 3 === 0)
<div class="item item2 {{ $i < 3 ? 'active' : '' }}">
#endif
What I understand is :
You have a $participatingProperties array and one div to print.
When it has 0 or 1 element, nothing happens.
When it has 2 elements, the div prints with a specific style.
When it has 3 or more elements, the div prints with another style.
(No rule for more than 3 elements).
I would keep it simple with count :
#php($count = count($participatingProperties))
#if ($count > 1)
<div
class="{!! ($count == 2) ? 'class-2' : 'class-3-or-more' !!}"
>
div content
</div>
#endif
If you don't need a specific style for every amount, it's even simpler :
#if (count($participatingProperties) > 1)
<div class="myclass">
div content
</div>
#endif
Using both empty and isset is redundant. And I think you should not have to check if $participatingProperties is an array, instead make sure it always is in your backend. If you return at least an empty array, count will return 0.
Related
I'm trying to display a specific set of references on my laravel page by iterating through an foreach loop and styling odd and even iterations differently with #class loop odd / loop even.
However I as I begin the foreach loop with an #if, only some make it into the loop while some are skipped, which messes up my odd / even (e.g. #1, #2 and #4 make it into the loop which means I have two elements styled with loop even.
How can I skip the ones that don't make it into the loop so I can display the result with alternating styling?
#foreach ($references as $reference )
#if(isset($reference['is_startpage'])
&& $reference['is_startpage'] != 1
&& isset($reference['content']['featured'])
&& $reference['content']['featured'] == 1)
<div #class([
'row py-5' => $loop->odd,
'row flex-row-reverse py-5'=> $loop->even,
])></div>
#endif
#endforeach
As you say, not every loop iteration prints the <div> element, so $loop->even and $loop->odd variables are not valid in your case for implementing that specific logic. However, there are many ways to achieve what you want. The first way that comes into my mind is using a specific counter for that, i.e.:
#php $featuredCount = 0; #endphp
#foreach ($references as $reference)
#if(
isset($reference['is_startpage'])
&& $reference['is_startpage'] != 1
&& isset($reference['content']['featured'])
&& $reference['content']['featured'] == 1
)
#php $featuredCount++; #endphp
<div #class([
'row py-5', // These two are always present, no need to conditionally rewrite them
'flex-row-reverse' => $featuredCount % 2 == 0, // 'Even' check
])></div>
#endif
#endforeach
I'm trying to loop data using foreach and I want to skip the html tag after the first item being looped.
I've tried like the code bellow, but the html tag <p> still being looped multiple time. What I want is the <p> tags only looped once
#foreach ($store_icon as $key => $icon)
#if ($key < 1)
<p class="available-at">Also available at:</p>
#endif
#endforeach
Result example:
What I want is like this:
Also Available at
- Product 1
- Product 2
But using the code above it resulted like this:
Also Available at
- Product 1
Also Available at
- Product 2
Thanks
I am not sure why it is not working, It must work and working the same code for as well, you can try --
#foreach ($store_icon as $key => $icon)
#if ($key == 0)
<p class="available-at">Also available at:</p>
#endif
#endforeach
OR you can use like this
#if(!empty($store_icon))
<p class="available-at">Also available at:</p>
#foreach ($store_icon as $key => $icon)
#endforeach
#endif
Whats problem? Just take it out from loop.
<p class="available-at">Also available at:</p>
#foreach ($store_icon as $key => $icon)
#endforeach
I am trying to implement a conditional operator on a value returned from controller to create some custom view.
front.blade
#if({{count($users)}} <= 5) <!-- if total number of rows in users table is less than or equal to 5 -->
<h3> total number of rows less than or equal to 5 </h3>
#endif
controller
$users = User::all();
return view('front', [ 'users'=>$users]);
and the error is
syntax error, unexpected '<'(View: \resources\views\front.blade.php)
Tried all permutation combination of putting the condition within {{ }} or quoting the operator or the constant value 5 the error remains same. I am new to laravel and this might be a fundamental mistake in regards to laravel or php.
Just remove {{ and }}, they're not needed besides a Blade directive (#ifin this case)
You need to remove the {{ }} inside the if condition.
Like this.
#if(count($users) <= 5) <!-- if total number of rows in users table is less than or equal to 5 -->
<h3> total number of rows less than or equal to 5 </h3>
#endif
first you need to understand when you need to use curly braces.when you display your data in blade file you need to use curly braces. like
Hello, {{ $name }}.
You may construct if statements using the #if, #elseif, #else, and #endif directives. These directives function identically to their PHP counterparts:
#if (count($records) === 1)
I have one record!
#elseif (count($records) > 1)
I have multiple records!
#else
I don't have any records!
#endif
Your Solution
#if(count($users) <= 5) <!-- if total number of rows in users table is less than or equal to 5 -->
<h3> total number of rows less than or equal to 5 </h3>
#endif
for details go into laravel documentation https://laravel.com/docs/7.x/blade#if-statements
In Controller change your code like this -
$users = User::all();
return view('front', compact('users'));
Blade file code-
#if(count($users) <= 5) <!-- if total number of rows in users table is less than or equal to 5 --><h3> total number of rows less than or equal to 5 </h3>#endif
I'm trying to iterate over a loop of items which is being included via the #each directive in Laravel Blade.
When I use a regular #foreach loop, this works perfectly fine and I can iterate over odd/even records, but when using #each, this concept doesn't seem to be working.
Am I doing something wrong or is this expected behaviour of the #each directive?
My code is as follows:
splits.blade.php
<section>
#each('_partials/components.split', $page->splits, 'split')
</section>
split.blade.php
<article
#if ($loop->odd)
style="background-image: url('placehold.it/1920x400');"
class="bg-cover bg-center"
#else
class="bg-white"
#endif
>
</article>
Any help would be greatly appreciated.
Many thanks
Matt
Each doesn't include $loop variable in blade file automatically but passes $key variable.
So you can write:
<article
#if ($key % 2 == 0)
style="background-image: url('placehold.it/1920x400');"
class="bg-cover bg-center"
#else
class="bg-white"
#endif
>
</article>
The each directive does not have the $loop variable like foreach.
However, it does send the key of every item in the provided array to the view. So if you did not set custom keys, the $key variable should contain a value from 0 to the length of your array.
Just found this out by looking in the source code.
I have a project where I'm pulling posts from the database and rendering on the home page view. Like a Blog. (On the home page I limit this to 3 posts)
I have 6 rows in the table, and would like to style the output based on ODD and Even rows.
Here is my controller:
public function index()
{
$counter = Post::count();
$posts= DB::table('posts')->orderBy('id', 'DESC')->limit(3)->get();
return view('home',compact('posts','counter'));
}
I want the even numbered rows to have <div class="even"> and the odd numbered rows to have <div class="odd">
When I dd on $counter I get the value 6. This is correct. I have 6 rows in the table.
What I'm currently trying based on other articles I've found:
#foreach ($posts as $post)
#if($counter % 2 == 0)
<div class="even">{{$post->title}}</div>
#else
<div class="odd">{{$post->title}}</div>
#endif
This doesn't do anything. Still outputs the rows as 6,5,4,3,2,1
So how can I write the IF Statement inside my Foreach loop to say...
if ($counter == odd)
<div class="odd">
else
<div class="even">
The order I'm looking for is:
Odd
Even
Odd
Even
$counter is a static variable, so calling $counter % 2 == 0 will always show the same result.
If you are using Laravel 5.4+, there is a $loop variable included in the #foreach(). So you can access your mod division within the loop.
Here is the example for Laravel 5.4+
#foreach ($posts as $post)
#if($loop->iteration % 2 == 0)
<div class="even">{{$post->title}}</div>
#else
<div class="odd">{{$post->title}}</div>
#endif
#endforeach
Laravel 5.8.5 add even and odd Boolean flags in the Blade loop variable
Now you can use:
$loop->even or $loop->odd
Instead of
$loop->iteration % 2
Reference link
You need to increment the counter on each iteration.
#php
$counter = 1;
#endphp
#foreach ($posts as $post)
#if($counter % 2 == 0)
<div class="even">{{$post->title}}</div>
#else
<div class="odd">{{$post->title}}</div>
#endif
#php
$counter++;
#endphp
#endforeach