tomczas
tomczas

Reputation: 179

laravel - if else loop not working proper

I'm trying to write an if-else loop: if there is an embed saved in the database it should display a calendar, if not it should display a button saying "add new calendar/add new embed", but it's not working. I can't figure out why. When there is an embed saved in the database it displays the embed but, if there isn't an embed it doesn't display the button to add a new calendar/embed:

Blade view:

@if (empty($calendars))

@else
    <h4>No calendar settings</h4><br>
    <a href="{{ route('calendarsettings.create')}}">
        <button class="btn btn-success btn-cons m-b-10" type="button">
            <i class="fa fa-cloud-upload"></i>
            <span class="bold">Add embed</span>
        </button>
    </a>
@endif
@foreach($calendars as $calendar)
    {{ $calendar->embed }}
@endforeach

Controller:

public function kalendarz() {
    $calendars = Calendar::with('users')->where('user_id', Auth::user()->id)->get();
    return view('kalendarz',['calendars'=>$calendars]);
}

Upvotes: 2

Views: 1794

Answers (4)

Jean-Philippe Murray
Jean-Philippe Murray

Reputation: 1248

In Laravel 5.4, I would probably use the @unless blade directive, that will show the block, unless a condition is met. What the following code would acheive is "show the entries if we have some, and show the button unless we have some entries".

@if($calendar)
    // show entries
@endif

@unless(empty($calendars))
    // show a button to add a new one
@endunless

It kind of is the same "logic" as with an else, but somehow I think it's more clear and easy to read than an @else statement, and make it separate from the if.

Upvotes: 1

Marwelln
Marwelln

Reputation: 29413

When using Eloquent to get results from a database you get an instance of Illuminate\Database\Eloquent\Collection. To check if that collection is empty you use $collection->isEmpty() or $collection->isNotEmpty() (L5.4).

@if ($calendars->isNotEmpty())
    // Have entries.
@else
<h4>No calendar settings</h4></br>
<a href="{{ route('calendarsettings.create')}}"><button class="btn btn-success btn-cons m-b-10" type="button"><i class="fa fa-cloud-upload"></i> <span class="bold">Add embed</span>
   </button></a>
@endif

@foreach($calendars as $calendar)
    {{$calendar->embed}}
@endforeach

Upvotes: 0

Advaith
Advaith

Reputation: 2580

empty() functions empty the variable $calendars, so try using count()

@if(count($calendars) > 0)
    <h4>No calendar settings</h4></br>
    <a href="{{ route('calendarsettings.create')}}">
        <button class="btn btn-success btn-cons m-b-10" type="button"><i class="fa fa-cloud-upload"></i> <span class="bold">Add embed</span></button>
    </a>
@else
    @foreach($calendars as $calendar)
        {{ $calendar->embed }}
    @endforeach
@endif

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163758

The correct syntax is:

@if(empty($calendars))
    <h4>No calendar settings</h4></br>
    <a href="{{ route('calendarsettings.create')}}">
        <button class="btn btn-success btn-cons m-b-10" type="button"><i class="fa fa-cloud-upload"></i> <span class="bold">Add embed</span></button>
    </a>
@else
    @foreach($calendars as $calendar)
        {{ $calendar->embed }}
    @endforeach
@endif

Upvotes: 1

Related Questions