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

Flex's avatar
Level 4

How can print data on table related to login user?

Hi, I have following table like students


id  name  email                 telephone         user_id
 1  anne   [email protected]   07115550096         5

user_id is related to id of users table.

now I need print all name, email and telephone data witch related to login user id. I have following controller function for that

public function index()
    {
        $students = Student::personal()->get();
        return view('students.index')->withStudents($students);
    }

and Student Model

 public function scopePersonal($query)
{
     return $query->where('user_id', Auth::user()->id);
      
}

using above syntax I am printing data as following,

{{Auth::user()->name}}
{{Auth::user()->email}}
{{Auth::user()->telephone}}

but it is not printing telephone value

0 likes
6 replies
Vilfago's avatar

Do you have an error ?

Is the authenticated user fill his phone number?

Vilfago's avatar
Vilfago
Best Answer
Level 20

@FLEX - I didn't see... you request on Auth::user(), and the information is on $students.

If you set the relation, you can try Auth::user()->student->telephone

1 like
Flex's avatar
Level 4

@vilfago how can set one to one relationship? may I use relationship on both models Student and User

Snapey's avatar

You load $students in the controller - why not just {{ $student-telephone }}

Please or to participate in this conversation.