Home > database >  select all rows with empty fileds Laravel
select all rows with empty fileds Laravel

Time:09-27

I wanted to fetch all rows from $result for which either skillId or subjectId fileds are null & when both are null.I've tried like below, it's not working as expected.How can i achieve this?

$result= Course::findOrFail($row);
if(empty($result['skillId']) || ($result['subjectId'])){
   
   $data['id']=$result['id'];
    $data['course']=$result['course'];
}

CodePudding user response:

$result= Course::where("skillId", null)->orWhere("subjectId", null)->get();

and now you don't need to check condition just fetch data.

$id=$result['id'];
$course=$result['course'];

CodePudding user response:

Try this solution:

$result= Course::where('id', $row)
                 ->where(function($query) {
                       $query->whereNull('skillId')
                      ->orWhereNull ('subjectId');
                  })
                 ->first();

if(!empty($result))
{
   return $result;
}

I tried this query it will work for me.

SELECT * FROM course
WHERE id = 1
and (skillId IS NULL OR subjectId IS NULL)

enter image description here

  • Related