phplaraveleloquentquery-builder

How to get records where column is not empty or null in laravel?


This is my table: enter image description here

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.


Solution

  • 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