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

ghaonga's avatar

Displaying user information of the same table

Greetings!, i have been struggling with this and I would appreciate any help. Below is the table view for project supervision. I want to display supervisor names instead of Id as show below. I have a single table for users (students and supervisors) which contains id, firstname, lastname, usertype and supervisor. So when i assigned student a supervisor, the supervisor (id) is added to a supervisor column and at the end i want to display their names instead of ID

      SNO	     Name	   Project	                             Supervisor       
        1 	Student1  	     File MS	                                 4 	               
        2 	Student3 	   Final Year Project MS 	                    8                   

Here is my codes: Table that display information.

       <table class="table" id="projectlist">
        <thead class=" text-primary">
          <th>SNO</th>
          <th>Name</th>
          <th>Project</th>
          <th>Supervisor</th>
          <th>Action</th>
        </thead>
        <tbody>
          @php
          $sno=0;
          @endphp
          @foreach ($submitted as $project)
          <tr>
            <td>{{++$sno}}</td>
            <td>{{$project->firstname}} {{$project->lastname}}</td>
            <td>{{$project->name}}</td>
            <td>{{$project->supervisor}}</td>
            <td>
              <a href="/user-assign/{{$project->userid}}" class="btn btn-default">Assign</a>
            </td>
          </tr>
          @endforeach
        </tbody>
      </table>

My controller:

         public function supervise($id)
        {
         $assignstudent = DB::table('users')
         ->join('project', 'project.userid', '=', 'users.id')
         ->where('users.id', '=', $id)
         ->first(); //get(); for many data

          $supervisor=DB::table('users')
          ->where('usertype','=','teacher')
          ->get();

          return view('admin.assign', compact('assignstudent','supervisor'));
}
0 likes
5 replies
Sinnbeck's avatar

Either join the table to itself (like you did with project.. Just remember to add a select) , or use eloquent with a relationship to itself :)

Sinnbeck's avatar

Query builder

$assignstudent = DB::table('users')
        ->select(['users.*', 'sup.name as super']) 
         ->join('project', 'project.userid', '=', 'users.id')
         ->join('users as sup', 'sup.supervisor', '=', 'users.id')
         ->where('users.id', '=', $id)
         ->first(); 
1 like

Please or to participate in this conversation.