0
I have two related tables in the database: Students and student_schedules. At some point, I want to list all the Students that NAY are in student_schedules. I tried to do it like this, but it didn’t work:
$schedules = StudentSchedule::all();
$students = Student::join('student_schedules', 'student_schedules.student_id', '!=', 'students.id')
->select('student_schedules.student_id as idd', 'students.*')
->get();
The image illustrates precisely what: When there is a student id in the student_schedules table, it does not appear in select
It will be "How to show data that is not in the student_schedules table?"
– Amadeu Antunes
Friend tries the following:
– Marcos da Cruz Sibilio Jr.
If you created the relationship in the model you can use doesntHave. Student::doesntHave('related_name em_model')->get(); But it only works if you set the related_name related_em_model in the model.
– Marcos da Cruz Sibilio Jr.
SQL would be something like this:
SELECT * FROM students WHERE student_id NOT IN (SELECT student_id FROM student_schedules);
, would have to convert to Laravel’s ORM– Costamilam
I edited the question to see if it is better to understand
– Edinho Rodrigues
These relationships are boring even have to be testing, but try so ve if right, Students::leftJoin('Students', 'Students.id', '!=', 'student_schedules.student_id')->get();
– Lodi