dimanche 26 janvier 2020

Cannot get data from collection in Laravel

I want to get data from collection i am getting following two arrays in collection when i run this code:


enter image description here

and i want to get data through loop for that i am using following code:

Code:

Category_repository:

public function getChildCategories($id)
{
    return Category::with('children')->find($id); 
}

Category Model:

   public function parent()
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }

    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }

Category Controller:

public function show($slug)
    {
        $category = $this->categoryRepository->findBySlug($slug);
        $child_categories=$this->categoryRepository->getChildCategories($category->id); 
        return view('site.pages.category', compact('category','child_categories'));
    }

View:

 @foreach ($child_categories as $index => $element)
  
@endforeach


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire