0

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

1 Answers1

0

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.

LevFlavien
  • 134
  • 1
  • 3