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

carcleo's avatar

I cant get the results with foreignkeys

I have the method

public function populateForm(Classroom $classroom, Student $student, User $user) {}

How can i get all the User that be Student inside this?

At the table students i have the user_id

And at User, i have too the relation

public function student() : HasOne {
    return $this->hasOne(Student::class);
}

Each student is the a user

Trying

dd($user->student()->toSql());

Out

"select * from `students` where `students`.`user_id` is null and `students`.`user_id` is not null" // app\Http\Controllers\ClassroomController.php:66

Therefore, i get zero results

1 like
4 replies
vincent15000's avatar
Level 63

Not sure to understand ... are you trying to get all users that are students ?

If yes, here is what you could do.

$students = Student::all();

Or via the user.

$users = User::has('student')->get();

Can you explain what you're trying to do with the populateForm() function with the classroom, the student and the user ?

Snapey's avatar

There is something wrong with your $user. How did you get that?

1 like

Please or to participate in this conversation.