laravel how to set class according to a message - php

I have a dropdown and I have styled it in a way that the current opened li is colored red.
This dropdown menu locates at the left of the page. and this menu exist in all pages.
All pages just set the content of other div in the page but the menu is always for all of them (I hope you get me).
In the controller I do this:
return View::make('restaurants.admins')->with('admin', $admin)->with('verticalMenu' , 'Admin');
So in the view I want to check this verticalMenu value, if it is admin, I will set admin's li class to open class and so on.
I will show you an example of what I want.
this is li that is closed
<li class="dropdown">
<a href="javascript:;">
<i class="fa fa-user"></i> Restaurant <span class="caret"></span>
</a>
<!-- more html here -->
</li>
This is li that is opened because it has the class open
<li class="dropdown active opened">
<a href="javascript:;">
<i class="fa fa-tasks"></i> Profile <span class="caret"></span>
</a>
<!-- more html here -->
</li>
So basically, I need to check the {{$verticalMenu}} before each li, right?, if yes, how please? if no, what is the correct way please?
After your answer
This is the mistake page, please look how the profile li is opened:
![Screen shot of the actual page][1]
This is the good one, where everything is perfect:

The {{ }} basically turns a code like this:
{{ $verticalMenu }}
Into this:
<?php echo $verticalMenu; ?>
What you are trying to achieve is this:
<?php echo $verticalMenu == 'Admin' ? 'active opened' : '' ?>
So the most straight-forward way to do it with blade syntax is:
<li class="dropdown {{ $vericalMenu == 'Admin' ? 'active opened' : '' }}">
<!-- The rest of the list content here -->
</li>

Related

Undefined variable $menus in laravel

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.

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

Highlighting selected tab in nav - laravel blade

I have the code below in my view, but when i route to the page, the tab isn't showing as active. I search for this on answer on SO but it wouldn't solve my problem..
What am i not doing right?
I am using laravel version 5.6
VIew
<ul id="main-menu-navigation" data-menu="menu-navigation" class="nav navbar-nav">
<li class="nav-item {{ Request::path() == '/admin/dashboard/foods/all' ? 'active' : '' }}"><a href="/admin/dashboard/foods/all" class="nav-link">
<img src="/images/menubar/items.png" width="30px" height="30px" border="0" alt="Module Icon"/>
<span>Food Items</span></a>
</li>
</ul>
First i recommend you to put the logic in a directive, so the html look creaner and the logic is separated from the view.
In your AppServiceProvider you just put this
Blade::directive('menuActive',function($expression) {
//explode the '$expression' string to the varibles needed
list($route, $class) = explode(', ', $expression);
//then we check if the route is the same as the one we are passing.
return "{{ request()->is({$route}) ? {$class} : '' }}";
});
know in your view you just add
<ul id="main-menu-navigation" data-menu="menu-navigation" class="nav navbar-nav">
<li class="nav-item #menuActive('admin/dashboard/foods/all', 'active')"><a href="/admin/dashboard/foods/all" class="nav-link">
<img src="/images/menubar/items.png" width="30px" height="30px" border="0" alt="Module Icon"/>
<span>Food Items</span></a>
</li>
I'm not sure what Request::path() is returning, but it doesn't match your check for /admin/dashboard/foods/all (honestly, likely an issue with leading /)
Also, there is a method called is() on the request() helper that determines if the current route matches a given value, so use as:
<li class="nav-item {{ request()->is('admin/dashboard/foods/all') ? 'active' : '' }}"> ...
And it should work.

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.

Categories