I am using the Laravel multi-auth for multiple users. I have frontend controller for the public access where no login is required. I want to check there if the user is logged in. If so I want to hide Login nav and show Home/Dashboard nav link which on click should redirect users to his dashboard. How can I check this in front-end controller/view? Any help is appreciated. Thanks in advance.
I have following guards:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'university' => [
'driver' => 'session',
'provider' => 'universities',
],
'school' => [
'driver' => 'session',
'provider' => 'schools',
],
'student' => [
'driver' => 'session',
'provider' => 'students',
],
],
Model:
<?php
namespace Modules\Student\Http\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Student extends Authenticatable
{
use Notifiable;
protected $guard = 'student';
protected $fillable = ['first_name', 'last_name', 'email', 'password', 'remember_token'];
public function applications()
{
return $this->hasMany("Modules\Student\Http\Models\Application", 'student_id');
}
public function languages()
{
return $this->hasMany("Modules\Student\Http\Models\StudentLanguage", 'student_id');
}
public function certificates()
{
return $this->hasMany("Modules\Student\Http\Models\StudentCertificate", 'student_id');
}
public function educations()
{
return $this->hasMany("Modules\Student\Http\Models\StudentEducation", 'student_id');
}
}
Controller:
<?php
namespace Modules\Student\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Modules\Admin\Http\Models\Country;
use Modules\Student\Http\Models\Student;
use Modules\Student\Http\Models\Application;
use Modules\Student\Http\Models\StudentCertificate;
use Modules\Student\Http\Models\StudentLanguage;
use Modules\Student\Http\Models\StudentDocument;
use Modules\Student\Http\Models\StudentEducation;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage;
class StudentController extends Controller
{
protected $redirectTo = '/student';
public function __construct()
{
$this->middleware('auth:student');
}
public function index()
{
return view('student::dashboard');
}
public function profileForm()
{
$countries = Country::get(['id', 'country_name'])->toArray();
$student = Student::with('languages')
->with('certificates')
->with('educations')
->with('educations.country')
->with('ducuments')
->find(Auth::User()->id);
$student->countries = $countries;
$student->docsGroup = $student->ducuments->groupBy('doc_type');
return view('student::student_profile')
->withStudent($student);
}
public function deleteEducation($id)
{
$language = StudentEducation::find($id);
if($language->delete()){
return 'success';
}
}
public function addCertificate(Request $request)
{
$this->validate($request, [
'certificate_name' => 'required',
'certificate_score' => 'required',
]);
$certificate = new StudentCertificate();
$certificate->student_id = Auth::id();
$certificate->certificate_name = $request->certificate_name;
$certificate->score = $request->certificate_score;
if($certificate->save()){
Session::flash('success', 'New certificate added successfully!');
return redirect()->route('student.profile');
};
Session::flash('success', 'Some error occured while addeding certificate!');
return redirect()->back()
->withInput()->with('tab', ['basic_info']);;
}
}
And this is my controller where I want to check if any student/admin.. logged in or not:
<?php
namespace Modules\Student\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
use Modules\Admin\Http\Models\Degree;
use Modules\Admin\Http\Models\ProgramCategory;
use Modules\University\Http\Models\Program;
use Modules\University\Http\Models\Scholarship;
use Modules\University\Http\Models\University;
use Modules\Admin\Http\Models\Country;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
class FrontendController extends Controller
{
// public function __construct()
// {
// $this->middleware('auth:student');
// }
public static function GetSearchAttributes()
{
$country_ids = University::where([
['status', '=', 'active'],
['id', '>', '0']
])
->get(['country_id'])
->pluck('country_id')
->toArray();
$countries = Country::orderBy('country_name', 'asc')
->whereIn('id', $country_ids)
->get(['id', 'country_name'])
->toArray();
$categories = ProgramCategory::orderBy('category_name', 'ASC')
->with('ProgramsCount')
->where([
['status', '=', 'active'],
['parent_id', '=', null]
])
->get(['category_name', 'id'])
->toArray();
$degrees = Degree::orderBy('degree_name', 'asc')
->where('status', '=', 'active')
->get(['degree_name', 'id'])
->toArray();
$searchAttr['countries'] = $countries;
$searchAttr['categories'] = $categories;
$searchAttr['degrees'] = $degrees;
$searchAttr = (object) $searchAttr;
return $searchAttr;
}
/**
* Display a listing of the resource.
* @return Response
*/
public function index()
{
$logos = University::orderBy('id', 'ASC')
->where([
['status', '=', 'active']
])
->limit(8)
->get();
return view('student::index')->withLogos($logos);
}
Use check()
@if (Auth::guard('student')->check())
///show link for student
@elseif(Auth::guard('admin')->check())
//show link for admin
@else
///show login
@endif
Do this for other guards
too. See this for better understanding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With