From 4612d11e3f1559b657b6b3351130d1160595c1d4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 10 Apr 2013 21:32:30 -0500 Subject: [PATCH] Allow "lazy" eager loading from an individual model: `$user->load('orders.lines');`. --- readme.md | 1 + src/Illuminate/Database/Eloquent/Model.php | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/readme.md b/readme.md index 2202e044e0a6..606f1442f2bf 100644 --- a/readme.md +++ b/readme.md @@ -14,6 +14,7 @@ - Implement sectionable caching across all drivers which support incrmeent / decrement. - Added `Redirect::home` method from Laravel 3. - Added `Crypt::setKey`, `Crypt::setCipher`, and `Crypt::setMode`. +- Allow "lazy" eager loading from an individual model: `$user->load('orders.lines');`. ## Beta 4 diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index b1eaa11d34e3..9bb094842060 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -370,6 +370,19 @@ public static function findOrFail($id, $columns = array('*')) throw new ModelNotFoundException; } + /** + * Eager load relations on the model. + * + * @param dynamic string + * @return void + */ + public function load() + { + $query = $this->newQuery()->with(func_get_args()); + + $query->eagerLoadRelations(array($this)); + } + /** * Being querying a model with eager loading. *