Skip to content
Advertisement

Laravel Relationships with multiple eloquent

Looks like I need some help with the Relationships part.For example, more than one address record of a user is kept in the database.

I can’t view the address part, I think I have a problem with eloquent, but I’m not sure.

Following my code:

user Table

JavaScript

adress Table

JavaScript

UserModel

JavaScript

AdressModel

JavaScript

UserController

JavaScript

user_migration

JavaScript

adres_migration

JavaScript

Advertisement

Answer

1 User having multiple addresse, so hasMany used and it will return a list of address. so you cant access as $user->adress;

  1. you can loop through $users->getAdress
  2. you can display comma separated address using pluck and join as below.

$users = User::with('getAdress')->get();

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement