Laravel 5.2属于与条件不起作用的关系

时间:2016-09-26 12:40:30

标签: php laravel laravel-5.2

我对belongsTo()关系的问题如下。

我的模特

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();
    }
}

我会收到错误column "object_type" does not exist

我该怎么办?

1 个答案:

答案 0 :(得分:6)

在关系功能中,请正确建模。

可能这也是一个错误。如果已经输入型号..   你有类型模型..?

在这种情况下,您需要使用多态概念。

polymorphic laravel 5.2