This question already has answers here:
Checking which `guard` is loggedin
(8 answers)
Closed 3 years ago.
I have here a portion of my header.blade.php file of a project.
#guest
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
#if(condition here)
$profile='/myprofile';
$logout='/logout';
#else
$profile='/admin/myprofile';
$logout='/admin/logout';
#endif
<ul class="dropdown-menu">
<li>My Profile</li>
<li> Log Out
</li>
</ul>
#endif
</li>
#endguest
I have 2 guards namely web and admin. I want the $profile and $logout to be '/profile' and '/logout' && '/admin/profile' and '/admin/logout' respectively for each of the 2 guards.
Now I know that I can check for each guard as Auth::guard('name')->check();
But, when both the guards are logged in at the same time, it creates a sort of problem this way.i.e.
if(Auth::guard('web')->check())
{$profile="/myprofile";}
if(Auth::guard('admin')->check())
{$profile="/admin/myprofile";}
Since both will be active, $profile will have the latter value in every case.
So, is there a way such that this problem can be addressed?
Something like
if(Auth::guard()=='admin'){
////////////////
}
which of course doesn't work.
I guess youll have to have separate headers for each of the interfaces taken care by each of your guards. Clearlyif(Auth::guard()=='admin'){
////////////////
} is not valid without mentioning the guard name
Related
This question already has answers here:
How to get client IP address in Laravel 5+
(20 answers)
Closed 2 years ago.
My site is trilingual ,I have a problem now, Normally, the site is in Persian language and users first see the Persian site and can change the language of the site from the language change option. How can the language be changed according to the IP of the visitor?
For example :Persian language users, by opening the site, view the site in Persian , Arabic-speaking users, by opening the site, see the site in Arabic .this is the method that used for change language.
controller:
public function index($locale) //en | fa
{
App::setLocale($locale);
Session()->put('local', $locale);
return redirect()->back();
}
app/config:
'timezone' => 'Asia/Tehran',
'locale' => 'fa',
'fallback_locale' => 'fa',
dropdown menu:
<div class="dropdown show nav-link">
#php $local =Session()->get('local'); #endphp
<a class="btn dropdown-toggle " href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
<img src="{{asset('images/translate/translate.png')}}" width="40px" height="40px">
#switch($local)
#case('fa')
فارسی
#break
#case('en')
English
#break
#case('ar')
العربی
#break
#endswitch
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item text-right nav-link " href="{{route('language','fa')}}"><img
src="{{asset('images/translate/iran.png')}}"
width="30" height="30"
>
فارسی</a>
<a class="dropdown-item text-right nav-link" href="{{route('language','en')}}"> <img
src="{{asset('images/translate/english.png')}}" width="30" height="30"
>English</a>
<a class="dropdown-item text-right nav-link" href="{{route('language','ar')}}"> <img
src="{{asset('images/translate/emarat.png')}}" width="30" height="30"
>العربی</a>
</div>
</div>
Rout
Route::get('lang/{locale}', 'LocalizationController#index')->name('language');
salaam.
You can get the user IP from request() method that is available in every request (every route). For example in your index method:
public function index($locale) //en | fa
{
$userIp = request()->ip();
App::setLocale($locale);
Session()->put('local', $locale);
return redirect()->back();
}
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.
I have just created multiple authentication laravel sucessfully. When i use these statement below, it has a little proplem.
#if(Auth::guard('student')->check())
<li><span style="color: white">Xin chào </span><a href="#" >{{Auth::guard('student')->student()->name}}</a></li>
#else
<li><a href="{{route('student.login')}}" >Login</a>
</li>
#endif
This Error is :
Method student does not exist.
Yes, i haven't yet created method student.
But if i change as {{Auth::guard('student')->user()->name}}.That's okay, it will display that name.
I don't know where it is i have to create student Method.
Inside the #if statement, you've already checked that a user with the guard 'student' is logged in, you don't need to check for that again, hence you can do something like this and it should work:
#if(Auth::guard('student')->check())
<li><span style="color: white">Xin chào </span><a href="#" >{{ Auth::user()->name }}</a></li>
#else
<li><a href="{{route('student.login')}}" >Login</a></li>
#endif
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
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>