1
votes

In one of my Controllers, I have the following:

return Lot::with(array('region', 'territory', 'manager')) -> get();

This works perfect, and returns the following:

Array
(
[0] => stdClass Object
    (
        [id] => 1
        [region_id] => 3
        [territory_id] => 2
        [state_id] => 5
        [manager_id] => 2
        [lot_num] => 0
        [lot_type] => managed
        [name] => Some Name
        [address_1] => Some Address
        [address_2] => 
        [address_3] => 
        [city] => Some City
        [zip] => 00000
        [opened_at] => 
        [deleted_at] => 
        [created_at] => 2014-11-06 00:49:39
        [updated_at] => 2014-11-06 00:49:39
        [region] => stdClass Object
            (
                [id] => 3
                [name] => Corporate
                [deleted_at] => 
                [created_at] => 2014-11-06 00:49:39
                [updated_at] => 2014-11-06 00:49:39
            )

        [territory] => stdClass Object
            (
                [id] => 2
                [name] => Corporate
                [deleted_at] => 
                [created_at] => 2014-11-06 00:49:39
                [updated_at] => 2014-11-06 00:49:39
            )

        [manager] => stdClass Object
            (
                [id] => 2
                [email] => [email protected]
                [active] => 1
                [last_login] => 
                [created_at] => 2014-11-06 00:49:39
                [updated_at] => 
                [deleted_at] => 
            )

    )

)

My 'manager' method in the Lot class has the following relationship:

return $this -> belongsTo('User');

My User class has the following method on it:

public function profile(){
    return $this -> hasOne('Profile');
}

Now, finally for my question :) Is it possible to eager load the Profile for this user through the eager loading I'm doing on my Lot class in the first code snip? What I'm trying to accomplish is to have my 'manager' object in the return look something like this:

[manager] => stdClass Object
            (
                [id] => 2
                [email] => [email protected]
                [active] => 1
                [last_login] => 
                [created_at] => 2014-11-06 00:49:39
                [updated_at] => 
                [deleted_at] => 
                [profile] => stdClass Object
                (
                    [id] => 5
                    [first_name] => Test
                    [last_name] => Tester
                    ...
                )
            )
1

1 Answers

6
votes

Use "dot" notation to eager load nested relationships:

return Lot::with(array('region', 'territory', 'manager.profile'))->get();