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

Sushant97's avatar

How do I access the data of a logged in user?

I am making a website for college administration where professors log in and assign marks to the students they are teaching.

There's a table, called "IA_Marks" in my database:

|Student_ID|Subject_Code|Name|Marks1|Marks2|Marks3|Semester|Division|

There's also a table called "Classroom_Mapper" in my database, that helps map a professor to a classroom, with a subject:

|Prof_ID|Subject_Code|Semester|Division|

My Problem: I wish to fetch the Subject_Code, Semester,Division fields from the classroom_mapper table so that I can use it to fetch the Student_ID,Name,Marks1,2,3 from the IA_Marks table.

To fetch the 3 fields from the mapper table I will need the Prof_ID field, which is unique for every professor. Every professor logs in using his Prof_ID.

Is there a way I can get the required fields to narrow down the select query results on IA_Marks, so that the professor can only see his students and not every student there is?

This is my current code: It shows the complete table, instead of just showing the records that are needed:

UserDefinedController.php

public function show(){
        
        $data = DB::table('iamarks')->get();
        return view('ia',compact('data'));
 }

ia.blade.php

<table>
        <tr>
                <th>ID</th>
                <th>SUBCODE</th>
                <th>NAME</th>
                <th>IA1</th>
                <th>IA2</th>
                <th>IA3</th>
                <th>SEMESTER</th>
                <th>DIVISION</th>
        </tr>
        @foreach($data as $row)
            <tr>
                <td>{{ $row->ID}}</td>
                <td>{{ $row->subcode }}</td>
                <td>{{ $row->name }}</td>
                <td>{{ $row->IA1 }}</td>
                <td>{{ $row->IA2 }}</td>
                <td>{{ $row->IA3 }}</td>
                <td>{{ $row->semester }}</td>
                <td>{{ $row->division }}</td>
            </tr>
        @endforeach

    </table>

I need to get the right values sent to my controller to add a WHERE clause to my query. Can you help me out?

0 likes
2 replies
Ozzfest's avatar

Did you try this?

public function show(){
        
        $data = DB::table('iamarks')->where('Prof_ID', auth()->user()->Prof_ID)->get();
        return view('ia',compact('data'));
 }

Please or to participate in this conversation.