Support the ongoing development of Laravel.io →
Database Blade
Last updated 2 years ago.
0

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
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

desbest desbest Joined 25 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.