In blade file using count ($sharedata->count() == 0)
In blade file using empty==(empty($sharedata))
In blade file using !$sharedata==if(!$sharedata)
In blade file using isset and !is_array($users)
@if(isset($users) && !is_array($users))
@if(!empty($profile_user_url->facebook))
conditionally set href and target based on hidden input field
apply ternary operator on value of hidden input field
We demonstrating best practices for handling empty data in Laravel applications. This example assumes a scenario where you are fetching data from the database using Eloquent ORM.
if ($sharedata->count() == 0):
This condition checks if the count of the $sharedata collection is equal to zero.
It is specific to collections in Laravel. The count() method is a method provided by Laravel's collection class, and it returns the number of items in the collection.
This condition is suitable when you are working with Laravel collections and want to explicitly check if there are no items in the collection.
if ($sharedata->count() == 0) {
// Code to handle an empty collection
}
if(empty($sharedata)):
This condition checks if the variable $sharedata is considered "empty" in a boolean context. In Laravel, an empty collection is considered truthy, so this condition might not work as expected with collections.
It is a more general-purpose check for emptiness that works with various types of variables, not just collections.
This condition can be used when you want to check if a variable is empty, regardless of its type.
if (empty($sharedata)) {
// Code to handle an empty variable
}
if (!$sharedata):
This condition checks if the variable $sharedata is falsy. In PHP, an empty collection is considered falsy, so this condition can be used to check for an empty collection.
It is a shorthand for if (empty($sharedata)), specifically for cases where you want to check if a variable is falsy.
if (!$sharedata) {
// Code to handle a falsy variable
}
Using is_array($users)
In Laravel Controller
public function myorder()
{
$currentURL = url()->current();
$id = Auth::user()->id;
$users = Paytm::where('admin_id', $id)->get();
log::info('now myorder' . $users);
log::info($users);
if ($users->isNotEmpty()) {
// $users contains data, so you can apply your additional conditions here
// For example, you can loop through the users and perform some actions
return view('pages.myorder', compact('users', 'currentURL'));
} else {
$users ="order is not assigned";
return view('pages.myorder', compact('users', 'currentURL'));
}
}
@if(isset($users) && !is_array($users))
<div class="container text-center mt-5" style="font-size: 50px; color:red;">
<h1>Order is not assigned</h1>
</div>
@else
<div class="container text-center mt-5" style="font-size: 50px; color:red;">
<h1> My order</h1>
</div>
@endisset
Based on server side value input field read only
we can solve this also jquery and javascript
@if(!empty($profile_user_url->twitter))
<div class="col-lg-6 col-md-6 col-sm-12 col-12">
<h5>Twitter</h5>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text fab fa-twitter" id="basic-addon1" style="font-size:24px; color: blue;"></span>
</div>
@if(!empty($pay_users))
<input type="text" name="twitter_price" id="twitter_url_price" class="form-control" placeholder="Enter Your Price/Post In Dollar" value="{{$pay_users->twitter_price}}" required>
@else
<input type="text" name="twitter_price" id="twitter_url_price" class="form-control" placeholder="Enter Your Price/Post In Dollar" value="" required>
@endif
</div>
</div>
@else
<div class="col-lg-6 col-md-6 col-sm-12 col-12">
<h5>Twitter</h5>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text fab fa-twitter" id="basic-addon1" style="font-size:24px; color: blue;"></span>
</div>
@if(!empty($pay_users))
<input type="text" name="twitter_price" id="twitter_url_price" class="form-control" placeholder="Enter Your first twitter url" readonly value="" value="{{$pay_users->twitter_price}}" >
@else
<input type="text" name="twitter_price" id="twitter_url_price" class="form-control" placeholder="Enter Your first twitter url" readonly value="" value="" >
@endif
</div>
</div>
@endif
Another Example
we can solve this also jquery and javascript
@if(!empty($profile_user_url->facebook))
<i class="fab fa-facebook" style="color: #00cccc;"></i> <span class="text-secondary"> <a class="text-secondary" href="{{$profile_user_url->facebook}}" target="_blank">Facebook</a><i class="fas fa-check" style="color: red;margin-left:5px;font-size:25px"></i> </span>
</h6>
@else
<i class="fab fa-facebook" style="color: #00cccc;"></i> <span class="text-secondary"> <a class="text-secondary" >Facebook</a></span>
</h6>
@endif
Using Jquery
<!-- Add a hidden input to store the value of $profile_user_url->facebook -->
<input type="hidden" id="facebookUrl" value="{{ !empty($profile_user_url->facebook) ? $profile_user_url->facebook : '' }}">
<h6>
<i class="fab fa-facebook" style="color: #00cccc;"></i>
<span class="text-secondary">
<!-- Anchor tag with ID 'facebookLink' -->
<a class="text-secondary" id="facebookLink">
Facebook
</a>
<i class="fas fa-check" style="color: red; margin-left:5px; font-size:25px"></i>
</span>
</h6>
<script>
// Get the Facebook URL from the hidden input
var facebookUrl = $('#facebookUrl').val();
// Get the anchor tag with ID 'facebookLink'
var facebookLink = $('#facebookLink');
// Check if the Facebook URL is not empty
if (facebookUrl) {
// Set the href and target attributes
facebookLink.attr('href', facebookUrl).attr('target', '_blank');
} else {
// Remove the href and target attributes
facebookLink.removeAttr('href').removeAttr('target');
}
</script>
In summary:
- If you are working with Laravel collections and want to check if there are no items, use $sharedata->count() == 0.
- If you want a general check for emptiness that works with various variable types, use empty($sharedata).
3. If you specifically want to check if a variable is falsy (including empty collections), use !$sharedata
Some More Examples
@isset($getting_roll_id)
@if ($getting_roll_id == 1)
.//data here
@else
//data here
@endif
@else
@endisset
@isset($totalNotificationCount)
{{ $totalNotificationCount }}
@else
0 {{-- or any default value you want to display --}}
@endisset
@if(isset($getting_notification))
@forelse ($getting_notification as $notification)
@php
@endforelse
@else
{{-- Handle case when $getting_notification is not set --}}
<div class="alert alert-warning" role="alert">
Variable $getting_notification is not set.
</div>
@endif
@if (isset($notificationCount) && $notificationCount > 0)
<span class="count" style="display: none;">
{{ $notificationCount }}
</span>
@else
@endif
@if (isset($notificationCount) && $notificationCount + $taskboard_notificationCount >= 5)
<a href="{{ route('AllNotifications') }}" class="btn btn-success" style="margin-left: 55px;">All Notification</a>
@endif
Top comments (0)