Laravel sidebar set active tab when page are open - php

How can I set active selected tab and and also open the sub tab if the page I'm on is a sub page on my sidebar? For example if I open Contractor Association page, the Users tab should expand the highlight Contractor Association tab. Right now every time I open a new page the sidebar is will reset back
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link {{ (request()->is('home')) ? 'text-primary' : '' }}"
href="{{ route('dev-admin.home') }}">
<i class="ni ni-tv-2"></i> {{ __('Dashboard') }}
</a>
</li>
<li class="nav-item">
<a class="nav-link {{ (strpos(Route::currentRouteName(), 'dev-admin.home') == 0) ? 'text-primary' : '' }}"
href="#users" data-toggle="collapse" role="button" aria-expanded="true" aria-controls="users">
<i class="fa fa-users"></i>
<span class="nav-link-text">{{ __('Users') }}</span>
</a>
<div class="collapse {{ (request()->is('dev-admin/developer-admins*') ? 'show' : '' }}"
id="users">
<ul class="nav nav-sm flex-column">
<li class="nav-item">
<a class="nav-link {{ (request()->is('developer-admins')) ? 'text-primary' : '' }}"
href="{{ route('dev-admin.developer-admins.index') }}">
{{ __('Admins') }}
</a>
<a class="nav-link {{ (request()->is('dev-admin/defect-types*')) ? 'text-primary' : '' }}"
href="{{ route('dev-admin.clerk-of-works.index') }}">
{{ __('Clerks of Work') }}
</a>
<a class="nav-link {{ (request()->is('developer-admins')) ? 'text-primary' : '' }}"
href="{{ route('dev-admin.developer-contractor-associations.index') }}">
{{ __('Contractor Association') }}
</a>
</li>
</ul>
</div>
</li>
</ul>

you can write the code like in side bar all the section
<li class="#if(Route::is('developer-admins')) {{ 'active' }} #endif"
<a href="{{ route('dev-admin.developer-admins.index') }}">
{{ __('Admins') }}
</a></li>
and also you can use like
<li class="{{ set_active(['login*']) }}">
Login
</li>
<li class="{{ set_active(['register*']) }}">
Signup
</li>

I am using bellow code for many project which is used into production ready web app and api.
Add bellow line into html class attributes for highlighting active route.
{{ request()->is('dashboard/report') ? 'active' : '' }}
This is really handy for active class.

So I fix it and also show/open tab for selected page by doing something like this, it appears the url I gave is incomplete, thanks everyone for the suggestion..
<div class="collapse {{ (request()->is('dev-admin/developer-admins*') || request()->is('dev-admin/clerks-of-works*') || request()->is('dev-admin/developer-contractor-associations*')) ? 'show' : '' }}"
id="users">
<ul class="nav nav-sm flex-column">
<li class="nav-item">
<a class="nav-link {{ (request()->is('dev-admin/developer-admins*')) ? 'text-primary' : '' }}"
href="{{ route('dev-admin.developer-admins.index') }}">
{{ __('Admins') }}
</a>
<a class="nav-link {{ (request()->is('dev-admin/clerks-of-works*')) ? 'text-primary' : '' }}"
href="{{ route('dev-admin.clerk-of-works.index') }}">
{{ __('Clerks of Work') }}
</a>
</li>
</ul>
</div>

Related

why logout don't show and i creat it with bootstrap auth in laravel?

here is the nav bar but the code is :
#else
<a id="navbarDropdown" class="nav-element" href="#" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
{{ Auth::user()->name }}
</a>
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{{ route('logout') }}"
onclick="event.preventDefault();document.getElementById('logout-form').submit();">
{{ __('Logout') }}
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
#csrf
</form>
</div>
</li>
#endguest
i can't logout
{{ __('Logout') }}
this div don't showed
I don't think you could use {{ __('Logout') }} as is. You may use {{ $Logout }} instead. To indicate that it came from a function in your file.
The code you pasted here is coming under #guest. So it will not show up if you are logged in. There will be a menu in your code for authenticated users add logout there

LARAVEL - Edit function in tab view

Asking for your help please. I want to show different Applicants on different tabs in my edit.blade.php, I just wondering how can I do that? Please help me.
Controller.php
public function edit($id)
{
$applications = application::where('id', $id)->first();
return view('user.application.edit', compact('applications'));
}
edit.blade.php
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link {{ request()->is('applicant1') ? 'active' : null }}" href="{{ url('applicant1') }}" role="tab">Main Applicant</a>
</li>
<li class="nav-item">
<a class="nav-link {{ request()->is('applicant2') ? 'active' : null }}" href="{{ url('applicant2') }}" role="tab">Applicant 2</a>
</li>
<li class="nav-item">
<a class="nav-link {{ request()->is('applicant3') ? 'active' : null }}" href="{{ url('applicant3') }}" role="tab">Applicant 3</a>
</li>
</ul><!-- Tab panes -->
<div class="tab-pane {{ request()->is('applicant1') ? 'active' : null }}" id="{{ url('applicant1') }}" role="tabpanel">
//FORM HERE
</div>
web.php I was trying different approaches on routes too.
Route::get('applicant1', 'ApplicationController#edit')->name('application.edit');
Route::get ('/applicant2', function () {
return view('application.edit');});
Route::get ('/applicant3', function () {
return view('application.edit');});
Please help me.
Why don't you use a foreach for each applicant element?
In your controller
public function edit($id)
{
$applications = application::all();
return view('user.application.edit', compact('applications'));
}
I your view:
<ul class="nav nav-tabs" role="tablist">
#foreach($applications as $applicant)
<li class="nav-item">
<a class="nav-link {{ request('id') == $applicant->id ? 'active' : ''}}" href="{{ route('application.edit', $applicant->id) }}" role="tab">Main Applicant</a>
</li>
#endforeach
</ul>
<div class="tab-content" id="myTabContent">
#foreach($applications as $applicant)
// the rest of panels
<div class="tab-pane fade show {{ request('id') == $applicant->id ? 'active' : '' }}" id="tab-{{ $loop->iteration }}" role="tabpanel"></div>
#endforeach
</div>
In your routes.php
Route::get('applicants/{id}', 'ApplicationController#edit')->name('application.edit');

Laravel - Laratrust - rename permissions name

Laravel Version: 7.6.2
Laratrust Version: 5.2.8
I have a Laravel 7.6 project and I use Laratrust for user roles and permissions. In my project, When i renamed permission name for example from management.dashboard.index to management-dashboard-index, but my web application not be loaded (It does not display an error).
Although I have cleared all the caches with php artisan cache:clear.
aside menu view :
#if ($user->can('management-dashboard-index'))
<li class="kt-menu__section ">
<h4 class="kt-menu__section-text">مدیریت</h4>
<i class="kt-menu__section-icon flaticon-more-v2"></i>
</li>
<li class="kt-menu__item kt-menu__item--submenu {!! (Request::is('management/panel/users*')) || (Request::is('management/panel/roles*')) || (Request::is('management/panel/permissions*')) ? 'kt-menu__item--open kt-menu__item--here' : '' !!}" aria-haspopup="true" data-ktmenu-submenu-toggle="hover">
<a href="javascript:;" class="kt-menu__link kt-menu__toggle">
<span class="kt-menu__link-icon">
<i class="la la-users"></i>
</span>
<span class="kt-menu__link-text">کاربران</span>
<i class="kt-menu__ver-arrow la la-angle-right"></i>
</a>
<div class="kt-menu__submenu ">
<span class="kt-menu__arrow"></span>
<ul class="kt-menu__subnav">
#if ($user->can('management.users.index'))
<li class="kt-menu__item {!! Request::is('management/panel/users*') ? 'kt-menu__item--active' : '' !!}" aria-haspopup="true">
<a href="{{ route('management.users.index') }}" class="kt-menu__link ">
<i class="kt-menu__link-bullet kt-menu__link-bullet--dot">
<span></span>
</i>
<span class="kt-menu__link-text">لیست کاربران</span>
</a>
</li>
#endif
#if ($user->can('management.permissions.index'))
<li class="kt-menu__item {!! Request::is('management/panel/permissions*') ? 'kt-menu__item--active' : '' !!}" aria-haspopup="true">
<a href="{{ route('management.permissions.index') }}" class="kt-menu__link ">
<i class="kt-menu__link-bullet kt-menu__link-bullet--dot">
<span></span>
</i>
<span class="kt-menu__link-text">پرمیشن ها</span>
</a>
</li>
#endif
#if ($user->can('management.roles.index'))
<li class="kt-menu__item {!! Request::is('management/panel/roles*') ? 'kt-menu__item--active' : '' !!}" aria-haspopup="true">
<a href="{{ route('management.roles.index') }}" class="kt-menu__link ">
<i class="kt-menu__link-bullet kt-menu__link-bullet--dot">
<span></span>
</i>
<span class="kt-menu__link-text">سطوح دسترسی</span>
</a>
</li>
#endif
</ul>
</div>
</li>
#endif
i get this message شما سطح دسترسی لازم را برای دیدن این صفحه ندارید
check the output of this part of your code $user->can('management-dashboard-index')
I think the answer will find if continue digging here.
start by seeing the result of {{ $user->can('management-dashboard-index') }}

how to show nav-item when opening special view laravel

how to show navbar only when i opening special view. example i have create this is app.blade.php
#guest
<li class="nav-item">
<a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
</li>
#if (Route::has('register'))
<li class="nav-item">
<a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
</li>
#endif
#Post
<div>post New</div>
#endPost
#else
#endguest
i want to showing those navbar only if i opening post.blade.php ini my view
Get the route name whois to load post.blade.php this view and then checked if the current route Route::currentRouteName(); match your routes.
//Example
#if('load_view_route_name' == Route::currentRouteName())
//Your Nav or Other elements
#endif

Laravel hasRole() error while creating user management

I am new to laravel5.4. By using this url i am trying to make user management. After installing everything i got below error. Please help this out to fix this error
(3/3) ErrorException
Call to undefined method Illuminate\Database\Query\Builder::hasRole() (View: /var/www/html/laravel5/resources/views/layouts/app.blade.php) (View: /var/www/html/laravel5/resources/views/layouts/app.blade.php)
My layout app.blade.php is as below
<ul class="nav navbar-nav navbar-right">
<!-- Authentication Links -->
#if (Auth::guest())
<li>Login</li>
<li>Register</li>
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
#role('Admin') {{-- Laravel-permission blade helper --}}
<i class="fa fa-btn fa-unlock"></i>Admin
#endrole
<a href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</li>
</ul>
</li>
#endif
</ul>

Categories