Flo Flo - 1 year ago 123
PHP Question

Laravel 5.3 get Data of relational database table

I got 2 Tables in my Database
First Table: "appointments" with the columns Id & userid
Second Table: "users" with the columns id & name

with blade I can easily show all appointments, but I want to show the users name & not only the id. So when I loop it with blade I could easy show the data with {{ $appointment->userid }}
But how can I get now for this appointment, the Users Name?

Answer Source

Define a relationship in your appointment class as:

public function user()
   return $this->belongsTo('App\User', 'userid');

Then in your blade you can do as:

{{ $appointment->user->name }}


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download