I tried to implement role base authentication. Authentication is done correctly. If the user is admin only then he can register a new employee. But the matter is the data put into registration form is not saved to database. Route redirections are correct but data is not saved. I am using builtin auth system of laravel 5.2.
Route:: get('/register',['middleware'=> 'roles', function(){
return view('auth.register');
}]);
middleware:
public function handle($request, Closure $next)
{
if ($request->user()==null)
{
return redirect('/login');
}
if (!$request->user()->isAdmin()){
return redirect('books');
}
return $next($request);
}
}
User Model:
public function roles(){
return $this->belongsToMany('App\Role','roles_users', 'user_id', 'role_id');
}
public function isAdmin() {
return in_array(2, $this->roles()->pluck('role_id')->all());
}
if I remove the middleware then the registered data is saved properly.