0

thanks for any input you all can give me on my problem. Long time lurker but this is my first post because honestly every question i have had about Laravel has been quickly solved with the abundance of info here and on stack overflow.

Let me first start by saying I had my menu working fine, until I started building the forum following a Laracast and finally learned how I could globally include a variable in my views using the app service provider. The code I have added there looks like this:

use App\Category;

... 

public function boot() {
 \View::composer('*', function($view){
            $categories = Cache::rememberForever('categories', function(){
                return Category::Roots();
            });

            $view->with([
                'categories' => $categories
            ]);
        });
}

I was really stoked to find this because this menu is upwards of 100 items in 5 main categories and lots of submenu and it really took along time to load, looping through each category using a @foreach in the blade template and the menu exists on every page of my app, so its just made the site very sluggish.

Unfortunately this has broken my multilevel menu, and really the whole site gets stopped from loading most of the time. :( now only the first root category shows up and opens and just shows itself again. Ithought it was maybe because the variable was getting passed in that individual view in the http controller still but removing it there hasn't helped, even after running php artisan optimize. I have literally been struggling to get it back to where it was before the change but even removing the above code and adding it back to the view the original view the first way doesn't seem to work. I am not sure how that's possible, but I also thought maybe my DB was messed up so I have remigrated and seeded a fresh copy and nothing changes. So strange because usually everything in Laravel just works... wtf.

I was thinking it could be possibly be that the cache was only storing a static version of the object and returning just the top level which is the other way it occasionally has loaded since ive been trying different things to get it working. But I also tried to add it with out caching. to the views like this (in the boot function of AppServiceProvider.php):


...
$categories = Category::Roots();
    View::share('categories', $categories);
...

So I have gotten frustrated at this point and was thinking maybe I need to refactor the whole menu so give me the data in different chunks or in a nested array so I can loop through the data like that but I would need a little guidance in what the most efficient and elegant way to load up this menu is. It doesn't need to change very frequently and wont be dynamic per user, it will stay the same site wide for the majority of its life, but i would like ti to be editable so the client can switch things as necessary, so a static menu is not going to cut it. Here is the blade files currently used to display this menu:

categories.blade.php

<ul class="cd-accordion cd-accordion--animated">
    @include('includes.subcategories', ['categories' => $categories])
</ul> 

and subcategories.blade.php

@foreach($categories as $cat)
    <div class="category">
        <a href="{{ route('category.show', $cat) }}" class="list-group-item category @if(isset($category) && $cat -> isAncestorOf(optional($category))) active @endif  align-items-center rounded-0 list-group-item-action d-flex justify-content-between">
            {{ $cat -> name }}
            <span class="badge badge-warning badge-pill">{{ $cat -> num_products }}</span>
        </a>
        @if($cat -> children -> isNotEmpty())
            <div class="pl-3 subcategories">
                @include('includes.subcategories', ['categories' => $cat -> children])
            </div>
        @endif
    </div>
@endforeach

I should also note I am using cache method above to load in channels to my forum globally, and all 108 of them load up no problem. I am assuming its how I am looping through the categories t print them but that is causing the issue... ?? maybe? I would really appropriate any insight.

Laravel 8 PHP 7.4 running locally with php artisan serve

thanks in advance, and just gotta say i love this site and Laravel is amazing so far, i don't get why some people that just start it hate on it so much its really not that confusing, but it saves so much damn time. anyways peace!

Mike Lucid
  • 1,344
  • 13
  • 26

1 Answers1

0

While reading "tree structure" and "performance issues" I would strongly recommend to switch to a nestedset + parent relation using for example

Gibhub: laravel-nestedset package

This will provide you with all tools/methods you need to handle tree structures and also the performance that is desirable.

patriziotomato
  • 591
  • 1
  • 11
  • 25