'Eloquent with three tables and apply condition on all include pivot

    $data = Department::with(['studentAdmissionRecord.academicRecord'])->where('id', $department_id)->first()

The tables are

(many to many relation) Table 1 -> departments(name, is_active)

Table 2 -> student_admission_record (name,fname,verify_status)

Pivot table

Table 3 department_student_admission_record (deparment_id,student_admission_record_id, type)

(one to one) student_admission_record has one academic_record

Table 4 academicRecord (marks,percentage)

now I want to apply the condition on student_admission_record for verify_status, and apply condition on academic_record for a percentage and apply condition on Pivot for type

$list = Department::with(['studentAdmissionRecords' => function 
    ($query) use ($type, $id) {
    $query->where('new_admission_id', $id)
    ->where('application_verified', 1)
    ->wherePivot('program_type', $type);
    }])->where('id', $department_id)->first();

I am using this but unable to make the connection between studentAdmissionRecords and academicRecord

Someone help me. This database is designed by a previous developer of this company.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source