1
$users = DB::table('users')
    ->join('course', 'users.id', '=', 'course.courseId')
    ->join('skills', 'users.id', '=', 'skills.id')
    ->join('subjects', 'users.id', '=', 'subjects.id')
    ->get();
 dd($users);

This gives me an empty result like this- items: []. I would like to join the 4 tables - users, course, skills & subjects and fetch all the data in all the 4 tables. users table has the id column(PK) named as id, course table has the id column(PK) named as courseId, skills table has its id column(PK) named as id & subjects table also has the id column(PK) named as id. The PK of all 4 tables is of the same data type biginteger. How to solve this?

3
  • do leftJoin instead of join or make sure you have data in all tables. Using relation would be easier if all you want is get all data (check answer) Commented Jul 30, 2021 at 8:50
  • @N69S All the 4 tables have data Commented Jul 30, 2021 at 8:51
  • Each user need to have data in all 4 tables, or do left joins. From documentation to perform a basic "inner join", you may use the join method. With inner join, if there is no row linked to one of the user in one of the other tables, the user will not be present in the result Commented Jul 30, 2021 at 8:54

1 Answer 1

2

Using the relations to get all the data:

The User::class has three relation methods hasMany() to the other classes (Course::class, Skill::class and Subject::class)

$users = User::with(['courses', 'skills', 'subjects'])->get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.