Richat CHHAYVANDY
Richat CHHAYVANDY

Reputation: 597

Laravel 5.2 belongsTo relationship with condition not working

I go problem with belongsTo() relationship as the following.

My Model

class TypeOne extends Model{

}
===============================
class TypeTwo extends Model{

}
===============================
class Post extends Model{
    public function typeOne(){
        return $this->belongsTo('Type','type_id')->where('object_type','type_one');
    }
    public function typeTwo(){
        return $this->belongsTo('Type','type_id')->where('object_type','type_two');
    }

    public function getPost(){
        return Post::with(['typeOne','typeTwo'])->get();
    }
}

And I will get error column "object_type" does not exist

What should I do with this?

Upvotes: 0

Views: 308

Answers (1)

Rama Durai
Rama Durai

Reputation: 750

In the relationship function, Please model as properly.

May be this is also make an error. If already you Type model.. Do you have Type model..?

You need to use polymorphic concept for this situation.

polymorphic laravel 5.2

Upvotes: 6

Related Questions