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

It's hard to understand your setup. Better show the tables, models or just the relations involved, then what you exactly want to achieve.

0

True...

User Model:

public function userable() {
	return $this->morphTo();
}

This is a polymorphic relation to various other models, eg 'UserFirm' the userfirm model contains various extra data specific to that type of user, including a hasOne relationship to a firm

UserFirm:

public function user()    {
        return $this->morphOne('User', 'userable');
}
	
public function firm(){
	return $this->hasOne('Firm', 'id', 'firm_id');
}

Then finally, the UserFirm model relates to a Firm Model

Firm:

public function userfirms(){
	return $this->hasMany('UserFirm');
}

using this setup, I can go to a firm, and then return a list of the 'userfirms' that belong to it, then get the 'user' for each firm 'userfirm', but i'd like to just return a list of users that belong to a firm

$userfirms = $this->hasMany('UserFirm')->get();			
		
foreach ($userfirms as $userfirm){			
	print $userfirm->user->first_name;
}

the above code works, but I want to return an object all all the 'user' data to the template, without having to loop through and get each user out of the database..

0

Sign in to participate in this thread!

Eventy

Your banner here too?

jonshutt jonshutt Joined 9 Dec 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.