Back

Create menu with variables from database


Hello, I would create a simple menu based on categories which are they creating from database. I have created model and I maked seeder with example data but I have problem with fetch this data to header view. Generally most my views based on strucured:

@include("header")  
<div class="container">       
      @yield('content')
       </div>
   </div>
@include("footer")

and in header.bade.php i would fetch these categories to my menu :

@foreach($categories as $c)
@if(($c->glowna) == 1)
<li><a href="#{{$c->id}},{{$c->tag_kategorii}}" class="parent-category">{{$c->nazwa_kategorii}}</a>
<ul class="submenu">
@foreach($categories as $sub) 
@if($sub->id_glownej_kategorii == $c->id)  <li><a href="#{{$sub->id}},{{$sub->tag_kategorii}}">{{$sub->nazwa_kategorii}}</a></li> @endif  @endforeach
</ul>
</li>
@endif
@endforeach

Problem is in controller, because each views have other controllers I dont want declarate variable $categories to all controllers in my project. I would that variable $categories which have data from Category table was creating only in one controller and will be visible in header which is using in all views.

Please help me and sorry for my english.

Firtzberg replied 1 week ago

Hi,

blade code is PHP code. You can fetch the categories inside the view instead of passing them in.

@foreach(...\Models\Category::all() as $c)

Firtzberg


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us