Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sozyar's avatar

As i said i'm new to laravel , i don't know about traits

Sozyar's avatar

I have tested this below code in AuthenticatesUser.php

protected function authenticated(Request $request, $user) { $type = $user->type; if ($type === 1) { return redirect('/back/permission'); } if ($type === 2) { return redirect('/back'); } return redirect('/login'); }

it works well but it uses to redirect me after logged in

i just want something like validate to prevent users to login by type

jlrdw's avatar

You still use the email and password for actual login. Users don't have access to "type". I am sorry if what you are wanting, I don't fully understand.

Sozyar's avatar

this was your code

public function authenticated(Request $request, $user) { $role = $user->role; if ($role === 'admin') { return redirect('pet/admin'); } if ($role === 'bkeep') { return redirect('account/index'); } return redirect('pet/index'); }

it seems you have role filed in users table that means you can distinguish users by role

in my case i have type field in users table instead of role field

Sozyar's avatar

Dear i have used permission and roles package , when you feel free just come to my pc using teamviewer to show you my project it will be very much clear to understand

jlrdw's avatar

You may want to put it on GitHub and start a new post that way others can have a look too.

Sozyar's avatar

If you understand by checking my project you may solve this easily

jlrdw's avatar

You said:

validate to prevent users to login by type

But you use type instead of role. Type should work no different than role. It's just a column name. If you mean form validation, the chapter is: https://laravel.com/docs/7.x/validation

Likewise if you want to protect a whole controller you can write custom middleware so only admin can do all methods in that controller:

public function __construct()
{
    $this->middleware('admin');   // just example
}

But in my authorization, and I have admin and bookkeeper, I did not have to write custom middleware. But it's there if you want to use it as well. https://laravel.com/docs/7.x/middleware

Previous

Please or to participate in this conversation.