2

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]);
}
1
  • Blade code block tells that "if there is no calendars do nothing" Commented Apr 9, 2017 at 7:43

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

6 Comments

still nothing if there's no calendar. In database embed is text field
@tomczas please show result of {{ dd($calendars) }}
it displays nothing
that's how database to store embed look like Schema::create('calendar', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned(); $table->string('embed'); $table->timestamps(); }); Schema::table('calendar', function (Blueprint $table) { $table->foreign('user_id')->references('id')->on('users'); });
any ideas? @Alexey Mezenin
|
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.