Within my model I wanting to add a role attribute that is a value based on what relationships the return user model has, so my user model has various relationships on it like below,
/*
* User - Supers
* 1:1
*/
public function super() {
return $this->hasOne('App\Super');
}
/*
* User - Teachers
* 1:1
*/
public function staff() {
return $this->hasOne('App\Teacher');
}
/**
* User - Students
* 1:1
*/
public function student() {
return $this->hasOne('App\Student');
}
What I am wanting to do is check if the user has a student or super relationship and set a role attribute based on that.
I thought I would be able to something like this,
public function getRoleAttribute() {
if($this->student()->user_id) {
return "Student";
}
//if($this->super)
}
but sadly not, the exception that gets return is this,
Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$user_id
does anyone have any idea how I can achieve this?