Laravel 5 属性 [userType] 在此集合实例上不存在
Laravel 5 Property [userType] does not exist on this collection instance
当我尝试设置登录条件时,我不断收到 属性 [userType] 不存在错误
下面是我的控制器代码。我也尝试制作 Auth 控制器,但我一直收到同样的错误。
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\User;
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';
}
}
}
这是我的模型代码
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
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('App\Post');
}
}
无论如何,我该如何解决这个问题?请告诉我在登录中创建重定向的正确方法,因为我只想创建一个具有管理员、用户和访客模式的登录。
很明显的错误
protected function redirectTo()
{
$user = User:all();
if($user->userType == 'admin')
{
return 'dashboard';
}
else
{
return 'verify first';
}
}
在 redirectTo
方法中您正在访问 $user = User:all();
而不是一条记录。它可能是 Auth::user()
或 User::find($id)
$user = User:all();
全部 return 多条记录所以你不能直接访问 userType
您可以使用 authenticated method
而不是 redirectTo
protected function authenticated(Request $request, $user)
{
if($user->userType == 'admin')
{
return redirect()->route("dashboard");
}
else
{
auth()->logout();
}
}
当我尝试设置登录条件时,我不断收到 属性 [userType] 不存在错误 下面是我的控制器代码。我也尝试制作 Auth 控制器,但我一直收到同样的错误。
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\User;
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';
}
}
}
这是我的模型代码
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
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('App\Post');
}
}
无论如何,我该如何解决这个问题?请告诉我在登录中创建重定向的正确方法,因为我只想创建一个具有管理员、用户和访客模式的登录。
很明显的错误
protected function redirectTo()
{
$user = User:all();
if($user->userType == 'admin')
{
return 'dashboard';
}
else
{
return 'verify first';
}
}
在 redirectTo
方法中您正在访问 $user = User:all();
而不是一条记录。它可能是 Auth::user()
或 User::find($id)
$user = User:all();
全部 return 多条记录所以你不能直接访问 userType
您可以使用 authenticated method
redirectTo
protected function authenticated(Request $request, $user)
{
if($user->userType == 'admin')
{
return redirect()->route("dashboard");
}
else
{
auth()->logout();
}
}