1 month ago

Counting models associated with category along with children.

Posted 1 month ago by jcc5018

Hey again, So I am still trying to figure out how to do this. I have managed to get the parents and child models for display when clicking on a parent category, but I am having trouble figuring out how to get the count of these models without having to run the whole loop again in a different method just to count things.

I have the parent count simple enough, but i need to also count the associated children models associated with each category.

Basically I need (Count of hobbies associated with parent + Count of hobbies where tag_id = $category->id through a M:M polymorphic relationship

in my Categories controller:

public function getCategories()

$categoriesCT = Tag::whereTag_type('80')    ->get();  
// $categoriesCT = Tag::withCount(['hobbies' ])
//     ->get();
    //    $categories=Tag::whereNull('tag_id')
    //            ->orderBy('tag')
    //            ->get();

// $hobbies = Tag::withCount('GetChildrenCategories')->get();

               return view('frontend/hobbies/browse',['categories'=>$categories, 'hobbycount'=>$hobbycount, 'categoriesCT'=>$categoriesCT]);


The view: Getting undefined offset errors

 @foreach($categories->whereNull('tag_id') as $category)

            <a href="/hobby/category/{{$category->tag}}">
                <div class="card card-grid border-dark mb-3">
                    <img src="..." class="card-img-top" alt="...">
                    <div class="card-body">
                        <h5 class="card-title">{{$category->tag}}</h5>
                        <p class="card-text">{{$category->description}}</p>
                        <p class="card-text"><small class="text-muted">{{$category->hobbies->count() }} +
                        @forelse ($categoriesCT->where('tag_id',$category->id)as $CCT)
                        {{$CCT->hobbies->count() }}


Tag Model -- categories are tag type 80

public function hobbies()
       return $this->morphedByMany('App\Hobby', 'tagable');

   public function tagable()
       return $this->morphTo();
   public function tag_type()
       return $this->belongsTo('App\EnumOption','tag_type'); // Tag Type id= 16


public function GetChildrenCategories()
       return $this->belongsTo('App\Tag','tag_id')->where('tag_type', 80);

    public function GetParentCategories()
       return $this->hasMany('App\Tag','tag_id')->where('tag_type', 80);
// recursive, loads all descendants
public function childrenRecursive()
  return $this->GetParentCategories()->with('childrenRecursive');

Hobby model incase i need to go about this the other way:

 public function tags()
        return $this->morphToMany('App\Tag', 'tagable');
     public function categories()
        return $this->morphToMany('App\Tag', 'tagable')->where('tag_type', 80);

Tags Table: id, tag,tag_id (parent), tag_type (80), description Tagables Table: tag_id, tagable_type, tagable_id

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