Php 在模型中找不到Laravel类
我正试图让我的项目使用授权角色来限制用户使用特定的功能,我正在跟随一个教程。当我调用user.php文件中的类时,我得到一个错误,即找不到类App\Role。我不确定这是否是一个名称空间问题,但我无法找到它的底部。我相信是角色职能给了我这个问题Php 在模型中找不到Laravel类,php,laravel,authentication,Php,Laravel,Authentication,我正试图让我的项目使用授权角色来限制用户使用特定的功能,我正在跟随一个教程。当我调用user.php文件中的类时,我得到一个错误,即找不到类App\Role。我不确定这是否是一个名称空间问题,但我无法找到它的底部。我相信是角色职能给了我这个问题 <?php namespace EliteWorker; use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use I
<?php
namespace EliteWorker;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'address', 'phone', 'email', 'password',
];
/**
* 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 roles() {
return $this->belongsToMany('App\Role');
}
public function hasAnyRoles($roles){
return null !== $this->roles()->whereIn('name', $roles)->first();
}
public function hasAnyRole($role){
return null !== $this->roles()->where('name', $role)->first();
}
}
您将名称空间更改为EliteWorker
,因此如果模型类角色
是使用Artisan生成的,那么它也将具有该名称空间
公共职能角色()
{
返回$this->belongtomany('EliteWorker\Role');
}
请注意,还可以通过调用类static属性来获取模型基名称
公共职能角色()
{
返回$this->belongstomy(角色::类);
}
如果它位于同一命名空间中,则无需导入它
还请注意,Laravel 6中删除了artisan命令app:name
,以鼓励开发人员使用通用app
命名空间