I keep getting property [userType] does not exist error when Im trying to make log in condition here is my Controller code below. I also tried to make Auth controller but I just keep getting the same error from this.
JavaScript
x
<?php
namespace AppHttpControllersAuth;
use AppHttpControllersController;
use AppProvidersRouteServiceProvider;
use IlluminateFoundationAuthAuthenticatesUsers;
use AppUser;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/dashboard';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
protected function redirectTo()
{
$user = User:all();
if($user->userType == 'admin')
{
return 'dashboard';
}
else
{
return 'verify first';
}
}
}
and here is my model code
JavaScript
<?php
namespace App;
use IlluminateContractsAuthMustVerifyEmail;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
* protected $table = 'users';
*public $primaryKey ='id';
* @var array
*/
protected $fillable = [
'name', 'email', 'password','userType',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function posts(){
return $this->hasMany('AppPost');
}
}
anyway on how can I fix this? Please show me the correct way to create a redirect in login because I just want to create a login with admin, user, and guest mode.
Advertisement
Answer
Its very clear erorr
JavaScript
protected function redirectTo()
{
$user = User:all();
if($user->userType == 'admin')
{
return 'dashboard';
}
else
{
return 'verify first';
}
}
in redirectTo
method you are accessing $user = User:all();
instead one record .It may be Auth::user()
or User::find($id)
JavaScript
$user = User:all();
all return multiple records so you cant access userType directly
Instead of using redirectTo
to you can use authenticated method
JavaScript
protected function authenticated(Request $request, $user)
{
if($user->userType == 'admin')
{
return redirect()->route("dashboard");
}
else
{
auth()->logout();
}
}