O
O
OlegGazmanov2020-05-10 16:11:48
Laravel
OlegGazmanov, 2020-05-10 16:11:48

How can I hide a block in Laravel?

There is one div inside which foreach works for me. Before this div I do a check

@if($events)
     <div class="card mb-5">
     ....

like if true then show the block, if false then don't show it. But the block is still visible, returns me an empty collection. How to fix it?

public function index($id) {
        $user = User::find($id);
        $events = $user->events()->orderBy('eventdata', 'desc')->limit(3)->get();

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan Ivanov, 2020-05-10
@OlegGazmanov

@if($events->count() > 0)
<div>....

J
jazzus, 2020-05-10
@jazzus

@if ($events->isNotEmpty())
 div
@endif

U
UksusoFF, 2020-05-10
@UksusoFF

Because $events is probably just a collection.
A collection is an object.
Therefore, you need to use collection methods to check if it is empty or not: https://laravel.com/docs/7.x/collections#method-is...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question