You aren't defining a relationship to "username". Instead you are defining a relationship to the User class. So first off, I would call the method "user" and not "username".
public function user()
{
return $this->hasOne('User', 'foreign_key', 'userid');
}
Then when you are getting your Hotel or set of Hotels you should eager load the User relationship.
In Your Controller
$hotels = Hotel::with(array('user'))->get();
In Your View (assuming the name is store in a column called "name" on the users table)
@foreach ($hotels as $hotel)
{{ $hotel->user->name }}
@endforeach
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community