just return $this if no conditions match.
Can a model HasOne relation depends on attribute ?
Hi all, I am not sure if I on the right track,
According to doc, Polymorphic relations allow a model to belong to more than one other model on a single association.
I am wondering can a model has either A or B relation on a single association ?
here is my case :
User has one "StaffInfo" or "VisitorInfo" ( just for an example) depends on their user_type
I was thinking of switch statement in relation method,
trying to get user's info on $user->info, returning different model depends on their user type
public function info()
{
switch ($this->getAttribute('user_type'))
{
case 'staff':
return $this->hasOne(StaffInfo::class);
case 'visitor'
return $this->hasOne(VisitorInfo::class);
default :
// for any other user type, shall not has any relation
// However I am getting LogicException with message 'Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation'
break;
}
}
It seems that relation method must have a model associated? Or there is a better approach for this case ?
Please advise, thank you so much
Please or to participate in this conversation.