how to create a sub menu on laravel in template view? - php

i want to create a sub menu on my templates/master. i want all my process in there. i have table: menu, role, and menu_role (conatins many to many from menu and role).
i tried to select table menu through menu_role, because i want display menu according to its role. it success but there is no sub menu. i'm bad at query builder with relationship.
this is my templates/master.blade.php :
use App\MenuRole;
use App\Role;
use App\Menu;
use App\Admin;
use App\Operator;
$userCheck = Auth::user()->role_id;
$menus = MenuRole::with('menu')->where('role_id', $userCheck)->get();
and for the html, if there is no module on table menu i want to display this :
#foreach($menus as $menu)
<li>{{ $menu->menu->title }}</li>
#endforeach
and if there is a module, i want to display this :
<li class="has-sub">
<a class="js-arrow" href="#">
<i class="fas fa-tachometer-alt"></i>{{ $menu->module }}</a>
<ul class="navbar-mobile-sub__list list-unstyled js-sub-list">
<li>{{ $menu->menu->title }}</li>
</ul>
</li>
so the thing is, in table menu i have field module. this field will distinguish which ones have subMenu and which have no subMenu. and i want to foreach table menu. i have configure the model.

Will a simple #empty condition suffice? When a Menu has the module field filled it shows one thing, otherwise the other.
#foreach ($menus as $menu)
#empty($menu->menu->module)
#else
#endempty
#endforeach
If this does not solve your problem, could you please go into more detail about what you want it to look like?

Related

How to foreach categories from 3rd index from my database / Laravel?

I am issuing a problem with the following:
I foreach-ed my categories from index 1 to 3 in my blade file.
Code
#foreach($categories->take(3) as $category)
<ul>
<li>{{$category->name}}</li>
</ul>
#endforeach
But now I want to foreach second half of my categories from my database on the other ul list.
How to do that..?
Thanks:)
You can use splice method
to popout the first 3 elements and then use another foreach to create another ul list from the other categories like below:
<?php
#foreach($categories->splice(0, 3) as $category)
<ul>
<li><a>{{ $category->name }}</a>
</ul>
#endforeach
#foreach($categories as $category)
<ul>
<li><a>{{ $category->name }}</a>
</ul>
#endforeach

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 5.2 filter with dropdownlist

I want to make drop-down list filtering.
I have a web page, that shown some post with title and categories.
The page has a drop-down in nav.blade.php. I dynamically generate drop-down from categories table. But when I select an item of drop-down (for example a category name) I want page to show me the posts only of that category. Also I have created the Category and Posts model and set the relationships. I can see all posts on my main page but can't filter content with drop-down list.
What I am doing wrong? and how can I solve this issue?
My nav.blade:
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
aria-haspopup="true" aria-expanded="false">Dropdown
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>#foreach($categories as $category)
<a href="{{URL::route('home',$category->id)}}">
<option value="{{$category->id}}">{{ $category->name }}</option>
</a>
#endforeach
</li>
</ul>
</li>
This would get you started:
Assuming you have a route like:
Route::get('/{category_id}', ['as'=>'home', 'uses'=>'PostController#show']);
In the PostController#show method:
public function show($category_id)
{
$categories = Category::all();
$selected_category = Category::with('posts')->where('id', $category_id)->first();
$posts = $selected_category->posts;
return redirect()->back()->with(compact('posts', 'categories'));
}
You can change the redirect location.

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