Specify the selected columns:
$students = Student::select('students.*')->join(...)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am trying to retrieve some "results" from a test in my students' grades database.
It is setup so that a student hasMany groups and groups have many results, then results belongTo a test.
I am trying to retrieve students who are attached to a group, and also any results from a test with id of test_id my queries are:
$students = Student::join('group_student', function ($join) use ($group_id) {
$join->on('group_student.student_id' , '=', 'students.id')
->where('group_id', $group_id);
})->leftjoin('results', function ($join) use ($test_id) {
$join->on('results.student_id' , '=', 'students.id')
->where('test_id', $test_id);
})->orderBy('last_name', 'asc')->get();
problem is that if there a results with an mark of null it works otherwise it returns null for important things like student_id.
Why does eloquent do this? how do I stop it from doing this again?
Any input appreciated!
You have to alias the duplicate columns:
Student::select('students.*', 'group_student.foo as group_student_foo', 'results.foo as results_foo', ...)
Please or to participate in this conversation.