0

In my coordinator_status column, there are values and null. When I run this code it only returns the ones with value only. I want to get null values too but I don't know how.

$coordetails = DB::table('courselist')
                        ->join('users','users.id','=','courselist.coordinator')
                        ->join('status','status.id','=','courselist.coordinator_status')
                        ->select('courselist.id','users.name','status.status')
                        ->where('courselist.faculty_id','=',$facultyid)
                        ->get();

2 Answers2

1

to do this, you should use leftJoin:

$coordetails = DB::table('courselist')
                        ->leftJoin('users','users.id','=','courselist.coordinator')
                        ->leftJoin('status','status.id','=','courselist.coordinator_status')
                        ->select('courselist.id','users.name','status.status')
                        ->where('courselist.faculty_id','=',$facultyid)
                        ->get();

The LEFT JOIN keyword returns all records from the left table , and the matching records from the right table if there are any.

OMR
  • 11,736
  • 5
  • 20
  • 35
0

according to me if you want to get null and value both data you have to use left join instead of join

you can find documentation on laravel site join in laravel

if you want to get courselist with joined user

DB::table('courselist')
->select('courselist.id','users.name','status.status')
->join('users','users.id','=','courselist.coordinator')
->leftJoin('status','status.id','=','courselist.coordinator_status')
->where('courselist.faculty_id','=',$facultyid)
->get();

if you want to get all courselist data without user relation then

DB::table('courselist')
->leftJoin('users','users.id','=','courselist.coordinator')
->leftJoin('status','status.id','=','courselist.coordinator_status')
->select('courselist.id','users.name','status.status')
->where('courselist.faculty_id','=',$facultyid)
->get();

and sql query will be like

SELECT * FROM testDb.course LEFT JOIN testDb.status ON testDb.course.status=testDb.status.id;

bhavesh
  • 453
  • 3
  • 11