Home » Php » php – How do i return data where that contain empty string instead of NULL in laravel

php – How do i return data where that contain empty string instead of NULL in laravel

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to get my data from database where one of my attribute was empty string instead of null….but my code return all data where NULL and empty string combined…here is my code

public function findAllPersons2(){
        return $this->where('pi_person_type','3')
                     ->where('pi_vendor_id','!=' , '')
                    ->orderBy('pi_name', 'asc')
                    ->get();
    }

How to&Answers:

Your code are using an not equal to an empty string condition. To retrieve all persons where pi_vendor_id’s is empty, do the following where condition.

->where('pi_vendor_id', '=', '')

Answer:

Try as below.

$this->where('pi_person_type','3')
                     ->where(function($q){
                         $q->whereNull('pi_vendor_id')->orwhere('pi_vendor_id','');
                     })

                    ->orderBy('pi_name', 'asc')
                    ->get();

Answer:

This code will also work:

->where('pi_vendor_id', '')

For convenience, if you want to verify that a column is equal to a
given value, you may pass the value directly as the second argument to
the where method.

https://laravel.com/docs/6.x/queries#where-clauses