Reputation: 483
This is my code I tried:
$socials = SocialIcon::whereNotNull('link')->get()->all();
$socials = SocialIcon::whereNotNull('link')->get();
I want to get all records where column link is not empty.
Upvotes: 1
Views: 18186
Reputation: 1072
Considering a value as NULL same as empty string is not correct. Both are not same.
You can use the below code:
$socials = SocialIcon::where(function($q) {
$q->whereNotNull('link')->orWhere('link','<>','');
})->get();
The resulted query running on DB will be:
select * from social_icons where (link is not null or link <> "")
If you wish to learn more about the Laravel's query builder, click here
Upvotes: 2
Reputation: 6233
First you have to understand the difference between NULL
and an empty string. NULL is the absence of a value and no memory is allocated for NULL. But empty string is a value with value stored in the memory as "". From your db I can see you have an empty string as a value for the last row in link column. If the value is NULL then you will find NULL is written in the field.
Now as you want to check both NULL and empty you should write it like
$socials = SocialIcon::whereNotNull('link')->orWhere('link','<>','')->get();
this query will both check for NULL and empty value and will return rows with not NULL and empty link value.
Upvotes: 6
Reputation: 483
You can simply do this
$socials = SocialIcon::where('link', '<>', '')->get();
Read this: https://laravel.com/docs/7.x/queries#where-clauses
Following will also do the job.
$socials = SocialIcon::all();
And then in your template for second case:
@foreach($socials as $social)
@if(!empty($social->link))
your content here
@endif
@endforeach
Upvotes: 0