Laravel 4 distant Eloquent relations
eloquent, laravel, laravel-4, mysql, sql
Solution
You use dot notation for nested relationships. It should make your life easier.
CorporateUsers::with('user.groups', 'corporations')->get();
Problem
I'm trying to eager load a distant relation using Eloquent and am running into problems. There are 5 tables involved, they are users, corporate_users, corporations and two Sentry tables (groups and users_groups). The tables are setup as follows: - Users hasOne CorporateUsers (one-to-one) - CorporateUsers belongsTo a Corporation (many-to-one) - Users have a many-to-many relationship with groups through the users_groups pivot table. All of these relationships work individually. Initially I could get around the distant relation problem by calling `CorporateUsers::with(array('user', 'corporations'));` because CorporateUsers have a direct relation with both Users and Corporations. My problem is that I how to setup the relationship between CorporateUsers and Groups, through the pivot table users_groups which references user_id and not corporate_user_id? I've tried the hasManyThrough relationship but it wasn't working. Anyone have any advice?