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

ghaonga's avatar

Displaying user information from the same table

Greetings, well I have a single table called users which has id and supervisor _id. The table is used for both students and supervisors. When i assigned a student a supervisor, the user id will be inserted as supervisor id. Now i want to display the info on a single like below. So i was wondering how would i go about into developing a laravel query that i will be able to display students and supervisors names. I managed to display student names and supervisor_id

STUDENT SUPERVISION Table SN |Student Name |Supervisor | 1 James Frank 7

Below is what i managed so far

   $student =DB::tables('users')
   ->where('usertype','=','student')
   ->get();

the view

        @foreach ($submitted as $project)
          <tr>
            <td>{{++$sno}}</td>
            <td>{{$student->firstname}} {{$student->lastname}}</td>
            <td>{{$student->supervisor_id}}</td> HERE  i want to display supervisor name. INSTEAD OF THEIR ID

. Assign @endforeach

0 likes
19 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Why not just use eloquent?

   $student = User::where('usertype','=','student')
  ->with('supervisor')
   ->get();

and

@foreach ($submitted as $project)
          <tr>
            <td>{{++$sno}}</td>
            <td>{{$student->firstname}} {{$student->lastname}}</td>
            <td>{{$student->supervisor->name ?? 'No supervisor'}}</td> 
         </tr>
@endforeach

For it to work you need to add this to the User model

public function supervisor()
{
    return $this->belongsTo(User::class, 'supervisor_id');
}
ghaonga's avatar

@Sinnbeck I get this error

Call to undefined method Illuminate\Database\Query\Builder::with()

ghaonga's avatar

@Sinnbeck this is want i wrote public function supervise($id) {

    $student = User::with('supervisor')->where('usertype','=','student')       
    ->get();
    return view('admin.assign', compact('student'));
}
Sinnbeck's avatar

@ghaonga That should work. Personally I would name it $students as you are getting all students, not just one

1 like
ghaonga's avatar

@Sinnbeck But the other thing, it is just a single table Users , there is no supervisor table. Could that be a reason? Example is the table below. I want to be able to display names of supervisor as well when i display data of students . Such as SN StudentName SupervisorName

    ID Name Usertype Supervisor
    1. Godfrey  Student   3
   2.  Jeff Student       3
   3.  Frank  tutor       null
Sinnbeck's avatar

@ghaonga yeah I completely understand what you mean. My code is written for that exact structure

ghaonga's avatar

@Sinnbeck well thank you for your help, but it stills producing the same error.

ghaonga's avatar
         @foreach ($students as $student)
          <tr>
            <td>{{++$sno}}</td>
            <td>{{$student->firstname}} {{$student->lastname}}</td>
           
            <td>{{$student->supervisor->firstname ?? 'No supervisor'}}</td> 
            <td>
              <a href="/user-assign/{{$project->userid}}" class="btn btn-default">Assign</a>
            </td>
          </tr>
          @endforeach
ghaonga's avatar

Bad Method Call

Did you mean Illuminate\Database\Query\Builder::when() ?

ghaonga's avatar

@Sinnbeck Illuminate\Database\Query\Builder::throwBadMethodCallException C:\xampp\htdocs\fypms\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php:71

Sinnbeck's avatar

@ghaonga can you share the error page? I assume there is a share button on the page?

ghaonga's avatar

@Sinnbeck THANK YOU SO MUCH....Now it works

->with('supervisor')

I changed this to a new name, it was interfering with the existing supervisor column

Please or to participate in this conversation.