Multi Auth Laravel - php

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

Related

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.

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

How to check which guard we are currently within in laravel [duplicate]

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

Laravel - Undefined variable when no data exists when filtered in database

I am having a struggle with an error I am getting: Undefined variable: orbitdata.
I have looked at some answers and I know this happens when there is no data in the database that matches the filter that I have in the controller. If there is data there is no problem.
The controller I am using is this:
$satellite = DB::table('satellites')->where('norad_cat_id', $norad_cat_id)->get();
$data = DB::table('satellites')->where('norad_cat_id', $norad_cat_id)->get();
$orbitalelements = DB::table('tle')->where('norad_cat_id', $norad_cat_id)->get();
return view('pages/satellite', compact($data, 'data', $orbitalelements, 'orbitalelements'));
The logic behind the controller is to filter the database by norad_cat_id and return the row which has the same norad_cat_id.
The 'tle' table might not have any data for the filtered norad_cat_id, so $orbitalelements returns no data. When it does not I get this error.
My blade file contains this foreach statement for the $orbitalelements variable:
#foreach($orbitalelements as $orbitdata)
#endforeach
I have tried adding an isset statement (which did not work):
#if(isset($orbitalelements))
#foreach($orbitalelements as $orbitdata)
#endforeach
#endif
How do you make it so that I don't get an Undefined variable error when there is no data from the database to return?
EDIT: Here is my full blade file:
<div class="satellite-content">
<div class="main-satellite-data">
<ul>
#foreach($data as $satellite)
<span id="satellite-name">{{$satellite->satname}}</span>
<li><span id="intldes">INTLDES: </span>{{$satellite->intldes}}</li>
<li><span id="noradid">NORAD Cat ID: </span>{{$satellite->norad_cat_id}}</li>
<li><span>Object Type: </span>{{$satellite->object_type}}</li>
<li><span>Country: </span>{{$satellite->country}}</li>
<li><span>Launch Date: </span>{{$satellite->launch}}</li>
<li><span>Launch Site: </span>{{$satellite->site}}</li>
<ul>
<li id="list-name"><span>ORBITAL ELEMENTS</span></li>
<li><span id="inclination">Inclination: </span>{{$satellite->inclination}}</li>
<li><span id="period">Period: </span>{{$satellite->period}}</li>
<li><span id="apogee">Apogee: </span>{{$satellite->apogee}}</li>
<li><span id="perigee">Perigee: </span>{{$satellite->perigee}}</li>
<li><span id="decay">Decay: </span>{{$satellite->decay}}</li>
#endforeach
#forelse($orbitalelements as $orbitdata)
<li><span id="epoch">Epoch: </span>{{$orbitdata->epoch}}</li>
<li><span id="meanmotion">Mean Motion: </span>{{$orbitdata->mean_motion}}</li>
<li><span id="eccentricity">Eccentricity: </span>{{$orbitdata->eccentricity}}</li>
<li><span id="meananomaly">Mean Anomaly: </span>{{$orbitdata->mean_anomaly}}</li>
<li><span id="bstar">Bstar: </span>{{$orbitdata->bstar}}</li>
</ul>
#empty
<p>no data</p>
#endforelse
</ul>
<div class="map-tle-padding">
<div id="satellitemap"></div>
<div class="tle-data">
<ul>
<li id="list-name-tle"><span>TLE Data</span></li>
#forelse($orbitalelements as $orbitdata)
<li id="tle-data-main">{{$orbitdata->tle_line0}}</li>
<li id="tle-data-main">{{$orbitdata->tle_line1}}</li>
<li id="tle-data-main">{{$orbitdata->tle_line2}}</li>
#empty
<p>no data</p>
#endforelse
</ul>
</div>
</div>
</div>
</div>
Results:
Without data: http://i.imgur.com/LNQCDLD.png
With data: http://i.imgur.com/27HD6qb.png
Laravel includes an option for this:
#forelse($orbitalelements as $orbitdata)
...
#empty
...No data...
#endforelse
Also, you are using compact wrong. It should just be:
return view('pages/satellite', compact('data', 'orbitalelements'));
#if(count($orbitalelements))
#foreach($orbitalelements as $orbitdata)
#endforeach
#endif
Should work since count($orbitalelements) count is zero, which is falsy.

laravel how to set class according to a message

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>

Categories