1

Am developing a website like Olx. I want to append the main categories and sub categories in menu bar. Am getting the return object like this for each element: enter image description here

My controller code is:

$all_categories = [];
        $main_categories_name = Category::where('parent_id', 0)->pluck('name')->toArray();
        $main_categories_id = Category::where('parent_id', 0)->pluck('id')->toArray();        
        foreach($main_categories_id as $id){
          $name = Category::where('id', $id)->pluck('name');
          $sub_categories = Category::where('parent_id', $id)->pluck('name')->toArray();
          $all_categories[] = array('name' => $name, 'sub_categories' => $sub_categories);
        } 
        return $all_categories;

How do I get these elements in blade?

@foreach($all_categories as $category) 
                @foreach($category['name'] as $category) 
                  <li id="{{$category}}" name="{{$category}}" class="dropdown-submenu main_category"> <a class="maincategory" tabindex="-1" href="#"><span class="categoryname">{{$category}}</span></a>
                    <ul class="dropdown-menu">
                      <li class="subcategory" id="subcat"> sub_category
                      </li>
                    </ul>
                  </li>
                @endforeach
            @endforeach

I used like this. Now I want sub_categories in the name where I mentioned sub_category.

Am getting like this: enter image description here

After using this code:

<ul class="dropdown-menu multi-level">
            @foreach($all_categories as $category)                 
              <li id="{{$category['name']}}" name="{{$category['name']}}" class="dropdown-submenu main_category"> <a class="maincategory" tabindex="-1" href="#"><span class="categoryname">{{$category['name']}}</span></a>
                <ul class="dropdown-menu">
                @foreach($category['sub_categories'] as $category) 
                  <li class="subcategory" id="subcat"><a href="#">{{$category}}</a></li>
                @endforeach
                </ul>
              </li>
            @endforeach
            </ul>

What to do for this? Am getting empty array like this? enter image description here

7
  • Call the blade file with compact of that variable. In the blade use foreach to format accordingly Commented May 29, 2017 at 5:53
  • return view('pages/home_page_product',compact('main','products', 'profile', 'all_categories','count','procat','probrand')); Commented May 29, 2017 at 5:55
  • I passed like this. How to call in blade? Commented May 29, 2017 at 5:55
  • 1
    In the blade, use foreach($all_categories as $category) and process Commented May 29, 2017 at 5:56
  • I did it. But getting this error. htmlspecialchars() expects parameter 1 to be string, array given Commented May 29, 2017 at 5:57

1 Answer 1

2

Return the view blade along with compact of that variable:

return view('yourblade',compact('all_categories'));

Then in the blade file, use foreach to parse the sub-categories:

foreach($all_categories as $category)

With formatting:

@foreach($all_categories as $category) 
    {{ $category['name'] }} // Prints men,... - format with your 
    @foreach($category['sub_categories'] as $subcategory) {
        {{ $subcategory }}  // Prints all your sub-categories under men,...
    @endforeach
@endforeach
Sign up to request clarification or add additional context in comments.

9 Comments

Everything perfect. Look at my updated answer. Am getting like that
Its the pluck that returns the array, use index to fetch [0] or pop() whichever you prefer
Yes. I did it. Thank you so much
Can you check my emptyness problem
Check if the array is not empty before processing your inner foreach
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.