아래 코드와 같이 아이 - 부모 관계에 사용자 및 역할 모델이 있습니다. 부모님을 통해 아이에게 접근 할 수는 있지만 그 반대는 불가능합니다. 아이의 역할 ($ user-> role)에 접근하면 ID 만받습니다. 역할 열에는 역할 테이블에 외래 키가 있지만 그 반대는 작동하지 않습니다. 는 기본적으로 $ 역할은 역할을 수행하면서, 사용자의 역할,Eloquent Model의 관계가 작동하지 않습니다. 쿼리조차도하지 않습니다.
Laravel-Debugbar 추가로 사용자가 별도의 쿼리를 수행하지 않음을 보여줍니다 만 자신의 ID를 표시하지 않습니다 모든 사용자 $ 사용자 -> 역할이 포함되어 있습니다.
사용자 테이블
id name email password remember_token client role created_at updated_at
역할 테이블
id name display_name description created_at updated_at
사용자 모델
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Laratrust\Traits\LaratrustUserTrait;
use App\Client;
use App\Role;
use App\Permission;
class User extends Authenticatable
{
use LaratrustUserTrait;
use Notifiable;
protected $fillable = [
'name', 'email', 'password','role',
];
public function client(){
return $this->hasOne('App\Client', 'client');
}
public function role(){
return $this->belongsTo('App\Role')->withDefault();
}
}
역할 모델
namespace App;
use Laratrust\Models\LaratrustRole;
use App\Permission;
use App\User;
class Role extends LaratrustRole
{
protected $fillable = [
'name', 'display_name', 'description',
];
public function GetHasPermission($perm){
return DB::table('permission_role')->where('role', $this->id)
->where('permission',$perm)->first()->value('type');
}
public function users(){
return $this->hasMany('App\User','role', 'id');
}
public function permissions(){
return $this->hasMany('App\Permission');
}
public function getName(){
return $this->name;
}
}
편집 : Laratrust를 사용하고 있습니다.
정확히. Laravel이 역할 대신 role_id를 찾고 있습니다 –
이미 시도했습니다. 올바른 것으로 생각 되더라도 작동하지 않습니다. –
@ Statharas.903 코드가 정확한지 확신합니다. 도움이 필요하면이 코드에 어떤 오류가 있는지 말해주십시오. –