Undefined variable $menus in laravel - php

I have a menu and in this menu items come from Categories
For the test, I create a new section called Menu for this menu to create the menu manually.
But after that, I return all codes to the previous version, But still, I have an error for undefined $menus
But I don't have this menu anywhere, where is the problem?
Error
Undefined variable $menus (View:
C:\Web\projects\thermotajhiz\resources\views\layouts\header.blade.php)
View
<ul class="list-menu-level-2">
#foreach($categories as $category)
#if($category->menu == 1)
<li class="item-menu-2">
<a href="#" class="list-category-menu-2" rel="nofollow">
<i class="fa fa-desktop"></i>
{{ $category->name }}
</a>
<ul class="megamenu-level-3" style="display:block;">
#include('layouts.categories-group',['categories' => $category-
>get_sub($category->id)])
</ul>
</li>
#endif
#endforeach
</ul>
Controller
$categories = Category::with('child')->where('parent_id', 0)->get();
return view('index', compact('categories'));

Please run a command php artisan view:clear, because laravel makes a cache for every view file. after runnig this command, all compiled views should clear and rebuild cache for view for present file.

Related

Laravel 7.14 loading social links with for each loop on header

I have a datatable for social links. a user can edit the link whenever he wants.
Just link any other crud it is very simple. I am trying to load all the links in blade view with icons in header of my app. I created as function and added a for each loop in the snippets.
here's my controller:
public function socials()
{
$socials = Social::all();
return view('inc.subheader')->with('socials', $socials);
}
Here's blade snippent:
<div class="col-md-3 justify-content-center social-links">
<ul class="nav d-flex justify-content-center">
#foreach ($socials as $social)
<li class="">
<i class="{{ $social->icon }} fa-2x"></i>
</li>
#endforeach
</ul>
</div>
The error I'm getting is "$socials is undefined" and when I go on localhost/inc/social I only see three dots and no icons with links.
Change this line:
return view('inc.subheader')->with('socials', $socials);
to this:
return view('inc.subheader', compact('socials'));
You can try all combination :
return view('inc.subheader', compact('socials'));
return view('inc.subheader', [ 'socials'=> $socials]);

How to make one submenu active in laravel blade?

Here I want to make one submenu active out of four submenu. This is for dynamic slug .
<div class="solution_tabs">
#foreach($allMenu as $menu)
<ul class="submenu">
#if(isset($menu->submenus))
#foreach($menu->submenus as $submenu)
#if(isset($submenu->page->slug))
<li class="active">{{ $submenu->name}}</li>
#else
<li>{{ $submenu->name}}</li>
#endif
#endforeach
#endif
</ul>
#endforeach
</div>
same as this image,i want one active submenu from dynamic submeun of the menu
You should compare the current URL with your link URLs in order to detect coincidences. One way to accomplish it could be injecting request in your blade file, using
#inject('request', 'Illuminate\Http\Request')
and then check for matches like this:
<li class="{{ $request->segment(1) == $submenu->page->slug ? 'active' : '' }}">{{ $submenu->name}}</li>
Please note that segment() is 0 based, so segment(1) works when your url is like example.com/segment(0)/page->slug

Undefined offset: 1 in CompilesLoops.php laravel 5

I have a services list. Now I want to add sub-services to services list. I have two tables 'services' and 'sub-services' with foreign key constraint 'service_id'. Now, I want to show the 'services' and related 'sub-services' in master.blade.php. For services it was working fine, but, when trying with sub-services then getting this error. Would someone please help to get the expected result.
In master.blade.php-
<li class="dropdown"> Services
<ul class="dropdown-menu services-dropdown" role="menu">
#forelse(App\Model\Service::all() as $service)
<li class="dropdown-submenu">
{{ $service->title }}
<ul class="dropdown-menu sub-services">
#foreach(App\Model\SubService::where('service_id', '=',$service->id)->get()) as $subservice)
<li>
{{ $subservice->title }}
</li>
#endforeach
</ul>
</li>
#empty
#endforelse
</ul>
</li>
Two tables are here-
1.Services table
2.Sub-services table
You're using the wrong syntax. You're using redundant ) near the get(), so change it to:
#foreach(App\Model\SubService::where('service_id', $service->id)->get() as $subservice)
Also, as I say in my best practices repo, you shouldn't execute queries in a Blade template. Consider moving the logic to a controller.
This error also occurs when you don't close you loop correctly.
Use #foreach() to start a loop and #endforeach to close the same loop.
Its a bad way to write a logic part in blade file.I would suggest you to move it to controller because if in case you need to change the code you have to edit blade page.As well as please make use of relationship for fetching the data you can relate your Service with SubService
E.g
Service.php (model file)
public function subServices()
{
return $this->hasMany('App\SubService');
}
SubService.php (model file)
public function services()
{
return $this->belongsTo('App\Service','service_id');
}
your blade code:
#forelse(App\Model\Service::all() as $service)
<li class="dropdown-submenu">
{{ $service->title }}
<ul class="dropdown-menu sub-services">
#foreach($service->subServices as $subservice)
<li>
{{ $subservice->title }}
</li>
#endforeach
</ul>
</li>
#empty
#endforelse

Laravel recursive partials view categories

I want to be able to display infinitive ammount of subcategories in my view for example I have in my blade view
#if (count($projects) > 0)
<ul>
#foreach ($projects as $project)
#include('partials.project', $project)
#endforeach
</ul>
#else
#include('partials.projects-none')
#endif
partials.project
<li>{{ $project['name'] }}</li>
#if (count($project['children']) > 0)
<ul>
#foreach($project['children'] as $project)
#include('partials.project', $project)
#endforeach
</ul>
#endif
projects-none
You have no projects!
but after I enter I get error message
Undefined variable: projects but when I dd($projects) i get data from database
Controller code
$projects= Projects::all(); (I tried it even with pluck but it didn't work)
return view('projects')->with('projects', $projects);
I have added dd($projects) after if loop in view to see if I'm receiving it all and I was receiving it;
I. With your "messy way"
- Change
#include('partials.project', $project)
- To
#include('partials.project', ['project'=>$project])
II. Try this better way. I though
- All in partials.projects
<ul>
#each ('partials.project', $projects, 'project', 'partials.projects-none')
</ul>
Similar to partials.project. I lot of code will be saved.

Multi-level menu in laravel 5.1 - loops

I am a little bit new with laravel 5.1 framework. Last couple of days I create my database (insert, update, delete) for dynamic menu I want to create. I connect with default layout in which i put menu. From route code looks like this.
View::composer('layouts.default',function($view){
$menus = Menu::where('parent_id',0)->orderBy('order')->get();
$submenus = Menu::where('parent_id','!=',0)->get();
$view->with(compact('menus','submenus'));
});
In main menu are items with parent_id = 0. Submenu items have parent_id = id, and soo on.
I want to display correct but when I hover main menu items that dont have items, css block appear, becouse i didnt make good if condition. Is there any way to do this?
Code in view look like this.
#foreach($menus as $menu)
<li class="dropdown {!! (Request::is('typography') || Request::is('advancedfeatures') || Request::is('grid') ? 'active' : '') !!}"> {!! $menu->title !!}
<ul class="dropdown-menu" role="menu">
#foreach($submenus as $submenu)
#if($submenu->parent_id === $menu->id)
<li>{!! $submenu->title !!}
#foreach($submenus as $smenu)
#if($smenu->parent_id === $submenu->id)
<ul class="dropdown-submenu" role="menu">
<li>{!! $smenu->title !!}
</li>
</ul>
#endif
#endforeach
</li>
#endif
#endforeach
</ul>
</li>
#endforeach
One more question is how to take only one value from Menu model for example id that can be used to point only one submenu.
Best regards!
You can do it simply by using recursion. I removed html classes for readability.
Note: Set NULL parent_id for root items.
Add this relation to your Menu Model.
function childs()
{
return $this->hasMany('Namespace\Menu','parent_id', 'id');
}
In your controller get the menus who has no parent.
$menus = Menu::whereNull('parent_id')->orderBy('order')->get();
Then display them in your view.
<ul>
#foreach($menus as $menu)
<li>
{!! $menu->title !!}
#include('childItems')//recursion view
</li>
#endforeach
</ul>
And this is what your childItems.blade.php will look like.
<ul>
#foreach($menu->childs as $menu)
<li>
{!! $menu->title !!}
#include('childItems')//call itself for deeper relations.
</li>
#endforeach
</ul>
That's it.

Categories