Flo Flo - 1 month ago 26
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

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 }}

Docs

Comments