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'));
}
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
<!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>
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>
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
@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..?