laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.7k stars 11.06k forks source link

[Request] Subrelationships on models #286

Closed Anahkiasen closed 11 years ago

Anahkiasen commented 11 years ago

One feature I always found missing in Laravel was some sort of support for subrelationships. Say I have a Blog model which has Article. Those articles themselves have Comment models. It would be nice to be able to do something like that :

class Blog
{
  public function comments()
  {
    return $this->hasMany('Article.Comment');
  }
}

Which would do something like that in the background :


class Blog
{
  public function comments()
  {
    $articles = $this->articles()->lists('id');

    return Comment::whereIn('article_id', $articles)->get();
  }
}

Although the above solution works fine it doesn't work with any already eager-loaded articles relationship nor does it allow me to use Eloquent's relationship syntax (ie. $blog->articles vs $blog->articles()).

bencorlett commented 11 years ago

+1.

What framework was this again this was from? It rings a bell. Was it Kohana or was it that redbean ORM?

taylorotwell commented 11 years ago

If you have an idea for how to implement it, feel free to open a proposal with some code examples of how it could be done in Eloquent. Personally I'm not inclined to implement it at the moment.