laravel user roles to access code example

Example 1: get user with role laravel

$students = User::whereHas(
    'roles', function($q){
        $q->where('name', 'Teacher');
    }
)->get();

Example 2: role acces login in laravel

public function authenticate(Request $request)
    {
        $request->validate([
            'email' => 'required|string|email',
            'password' => 'required|string',
        ]);

        $credentials = $request->only('email', 'password');

        if (Auth::attempt($credentials)) {
           
                return redirect('/');
            }          
            return redirect('login')->with('error', 'Opps! You have entered invalid credentials');   
              
    }

Tags:

Php Example