Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Neeraj1005's avatar

How to check if else condition in blade laravel

I am simply trying a if else condition in blade but not get output for else if data is blank. After running this code. if condition is running properly but if data is blank else condtion is not running. So, can anyone tell me where did I mistake? This is my view file

<!DOCTYPE html>
<html>
<head>
    <title>UserTrash</title>
</head>
<body>
    <div>
        
           @foreach($userdata as $user)
            
            @if ($user != null)
            
                    <h3>{{$user->name}}</h3>

                    <h3>{{$user->email}}</h3>
            
            @else

            There is no record

            @endif

        @endforeach
    </div>
</body>
</html>

and this is my controller file function

    public function trash()
    {
        $userdata = User::onlyTrashed()->get();
        return view('admin.trash.usertrash.bin', compact('userdata'));

    }
0 likes
11 replies
bobbybouwmann's avatar

This should work just fine! The only reason I can think of is that $userdata only holds users. If I look at your query in the controller this is correct. The query will never return an empty user or null object, so your else will never be reached!

I believe you want something else. If there are no users in the $userdata variable you want to display the else. Try this

@if (count($userdata)) 

    <h3>{{$user->name}}</h3>
    <h3>{{$user->email}}</h3

@else

    There is no record

@endif
1 like
Neeraj1005's avatar

@bobbybouwmann else is running but when data is empty. But if condition gives me an error.

Undefined variable: user (View: C:\wamp64\www\CMS\resources\views\admin\trash\usertrash\bin.blade.php)
Sinnbeck's avatar

Try this

<!DOCTYPE html>
<html>
<head>
    <title>UserTrash</title>
</head>
<body>
    <div>
        
           @foreach($userdata as $user)
            
            @if ($user)
            
                    <h3>{{$user->name}}</h3>

                    <h3>{{$user->email}}</h3>
            
            @else

            There is no record

            @endif

        @endforeach
    </div>
</body>
</html>
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Just looked at your query. Don't see any reason why user would ever be null? Did you mean to just show that message if there are no users at all?

<!DOCTYPE html>
<html>
<head>
    <title>UserTrash</title>
</head>
<body>
    <div>
        @if ($userdata->count())
           @foreach($userdata as $user)
          
            
                    <h3>{{$user->name}}</h3>

                    <h3>{{$user->email}}</h3>
            
            
        @endforeach
          @else

            There is no records

            @endif

    </div>
</body>
</html>
Sinnbeck's avatar

Good then my last answer will do it. It checks if there are any users and if not renders the text (edit : fixed typo in code)

1 like
Cronix's avatar

There is also @forelse, which is a bit less code. It was made basically for this scenario and you don't need an if() check. It does that behind the scenes.

@forelse ($users as $user)
    <h3>{{$user->name}}</h3>
    <h3>{{$user->email}}</h3>
@empty
    <p>There are no users.</p>
@endforelse

https://laravel.com/docs/5.8/blade#loops

2 likes
Neeraj1005's avatar

@sinnbeck @cronix This is my view file: Here I want to display total number of users in section. So how can I do this. Should we have to write line of code in controller..?

      <section class="content-header">
       <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h3>UsersTrash&nbsp;
              <span class="badge badge-secondary" style="font-size: small;">TotalTrash
              </span>
            </h3>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

<div class="card">
                <div class="card-body">
                    <table id="example1" class="table table-bordered table-striped">{{--Table start--}}
                        @forelse($userdata as $user)

                            <thead>
                                <tr>
                                    <th>S.No</th>
                                    <th>User-ID</th>
                                    <th>Name</th>
                                    <th>Email</th>                       
                                </tr>
                            </thead>

                            <tbody>
                                <tr>
                                    <td>{{ $loop->index+ 1 }}</td>
                                    <td>{{ $user->id }}</td> 
                                    <td>{{ $user->name }}</td>
                                    <td>{{$user->email}}</td>
                                </tr>

                                    </tbody>

                                    <tfoot>
                                        <tr>
                                            <th>S.No</th>
                                            <th>User-ID</th>
                                            <th>Name</th>
                                            <th>Email</th>
                                        </tr>
                                    </tfoot>
                        @empty

                        <td>Users Trash is Empty.</td>

                        @endforelse 

                            </table>{{--Table close--}}                  
                </div><!--body-->
          </div>

and this is my controller file function

    public function trash()
    {
        $userdata = User::onlyTrashed()->get();
        return view('admin.trash.usertrash.bin', compact('userdata'));

    }
Sinnbeck's avatar

You can easily do

<div>There are {{$userdata->count()}} users</div>
1 like

Please or to participate in this conversation.