Counting grandchildren

Posted 3 weeks ago by bwrigley

I'm looking through the eloquent documentation and I'm not quite finding what I woud like. hasManyThrough seems to be the closest but doesn't quite cut it.

So in my app a User owns many ForumThreads

class User extends Model
{
    public function forumThreads(): HasMany
    {
        return $this->hasMany('App\Forum\ForumThread');
    }
}

and a ForumThread can have many ForumLikes

class ForumThread extends Model
{
    public function forumLikes(): MorphMany
    {
        return $this->morphMany('App\Forum\ForumLike', 'likeable');
    }
}

What I had naively hoped was that I could say

$userLikes = $user->forumThreads->forumLikes->count();

is there an eloquent way to do this or do I need to simply loop through?

Thanks!

Please sign in or create an account to participate in this conversation.