Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Cryank's avatar

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

0 likes
7 replies
Snapey's avatar

just return $this if no conditions match.

Cryank's avatar

@Snapey do you mean

    public function info()
    {
        switch ($this->getAttribute('user_type'))
        {
            case 'staff': 
                return $this->hasOne(StaffInfo::class);
            case 'visitor'
                return $this->hasOne(VisitorInfo::class);
            default :
            return $this;
        }
    }

still throwing LogicException with message 'Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation'

Snapey's avatar

Well thinking more about your issue

switch ($this->getAttribute('user_type')) $this is not your model, its an instance of Relation class so you cannot inspect its attributes.

However this does not explain the error.

You just need to follow the guidance for polymorphic models.

vipin93's avatar

i think there is no need of using condition, just defined relationship hasOne in your both model and call as user_type

Cryank's avatar

@Snapey

I tried to dd(this) and it gives me my model,

about polymorphic, doc talks about Comment can belongs to 'Post' and 'Video' (If I get it correctly)

However in my case is like Post can have ModelA or ModelB in single association $post->comment

so I stuck at defining the relationship in Post

   public function comment()
    {
    // Model A or ModelB 
        return $this->morphOne('App\Comment', 'commentable');
    }
Cryank's avatar

@vipin93 do you mean I have to declare two relation method staffInfo() and visitorInfo(), then get $user->staffInfo or $user->visitorInfo depends on $user->user_type ?

Please or to participate in this conversation.