lundi 5 avril 2021

Laravel leftJoin query fetching duplicate/missing record

I have table structure below. I am trying to fetch data of Gurdian_name. Some column are NULL, they should be not anyway. It's on production environment. It's impossible to fix the manually. I want to fetch all the data without duplication or missing.

Note: NULL's real value on braces.

Student table
id | name      
1    Jack
2    John

Student Program table
id | Program_Name | s.id   
1    CS              1
2    EEE             1

Relationship table

id | relationship_name      
1    father  
2    mother   

Relationship_Contact table

id | rel_id  | phone   | student_id | Gurdian_name
1    NULL(1)     012345     1     Alison
2    2        543210     1        Alisa

Student_Relationship table

id | rel_id  | student_id
1    1           1
2    NULL(2)     1

So far I write this query below:

$query = DB::table('students AS s')
            ->leftJoin('student_programs AS sp', function ($join) {
                $join->on('sp.student_id', '=', 's.id')
                    ->whereNull('sp.deleted_at');
            })
            ->leftJoin('student_relationships AS stucontrel', 'sturel.student_id', '=', 's.id')
            ->leftJoin('student_contacts AS stucont', 's.id', '=', 'stucont.student_id')
            ->leftJoin('relationships AS rel', 'rel.id', '=', 'stucont.relationship_id')
            ->get();

Is there anyway I can add student_relationships with relationships on query so no data go miss?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire