Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

masternz06's avatar

Laravel Relationship newbie

please help i cannot do it in Join query.. i have 3 tables employees, employee_qualification and qualification db. please see my db structure below. i want to connect the 3 but i dont know what to do using hastomany or any eloquent relationship. im newbie sorry.

employees database id name rank 1 nikko csa 2 jhen supervisor

employee_qualification database id emp_id qualification_id 1 1 2 2 1 3

qualification database id qualification 1 counter 2 gates 3 boarding

im trying to do this in query builder

$schedule = $request['schedule'];

    $csa_query =
        DB::table('employees')
        ->Join('emp_qualification', 'employees.id', '=', 'emp_qualification.emp_id')
        ->where('rank','=','csa')
        ->where(winter_sched,'=',$schedule)
        ->orderByRaw("RAND()")
         ->select('employees.*','emp_qualification.emp_id');

$csa = $csa_query ->get(); return $csa_row_cnt = $csa_query->count(); //result is 4 but i only have 2 employees with csa rank. 4 is the sum of their qualifications.

0 likes
0 replies

Please or to participate in this conversation.