Skip to content

Ability to load count relations on Illuminate\Database\Eloquent\Collection #17845

@ghost

Description

  • Laravel Version: 5.4.10
  • PHP Version: 7.0.13

Description:

It is possible to load relations to models in a Collection but it seems not to be possible to load count relation on a Collection.

For example, if I have these classes

class Post extends Model {
    public function tags() {
        return $this->hasMany(Tag::class);
   }
}
class Tag extends Model {
    public function posts() {
        return $this->hasMany(Post::class);
   }
}

and assuming that $tags is a Collection of Tags, I can do $tags->load('posts') to load relation posts on every tag on $tags but I cannot do $tags->load('posts_count') to load posts_count.

Is there a way to do that ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions