Php 雄辩的模型继承-多表设计

Php 雄辩的模型继承-多表设计,php,laravel,inheritance,eloquent,Php,Laravel,Inheritance,Eloquent,假设我们有两个db表:posts和threads: threads id - integer title - string posts id - integer body - text created_at - timestamp thread_id - integer (fk) 和两个雄辩的模型: class Post extends Model { public function thread() { return $thi

假设我们有两个db表:
posts
threads

threads
   id - integer
   title - string
posts
   id - integer
   body - text
   created_at - timestamp
   thread_id - integer (fk)
和两个雄辩的模型:

class Post extends Model { 

   public function thread()
   {
       return $this->hasOne('App\Thread');
   }  
}


class Thread extends Post { 

   protected $table = 'threads';

   public function post()
   {
      return $this->belongsTo('App\Post');
   } 
}
我想要实现的是
Thread
对象具有
id、title、body、created\u at
属性,而
Post
对象具有
id、body、created\u at
属性

然而,我仍然得到了错误:

未找到列:“字段列表”中的1054未知列“正文”


这是MySQL错误,基本上意味着Laravel正在尝试查找
threads
表中的
body
列。但是,它存储在
posts
表中。

对于雄辩来说,您试图做的事情是不必要的

从数据库检索的数据存储在
$attributes
属性中


因此,除非你在分享任何行为,否则你不需要继承任何东西,除了
模型

你试图做的事对于雄辩来说是不必要的

从数据库检索的数据存储在
$attributes
属性中


因此,除非你分享任何行为,否则你不需要继承任何行为,除了
模型

好的,在对你的问题进行了一些研究之后,这里是我的最新答案。正如您定义的表模式,即

threads
   id - integer
   title - string
posts
   id - integer
   body - text
   created_at - timestamp
   thread_id - integer (fk)
这意味着
Thread
有多个或只有一个(取决于关系)
Post
,而
Post
属于
Thread
,因此关系如下

class Thread extends Model{ 
   protected $table = 'threads';
   public function post()
   {
      return $this->hasMany('App\Post');//Or hasOne('App\Post')
   } 
}
class Post extends Model { 
   public function thread()
   {
       return $this->belongsTo('App\Thread');
   }  
}
您想要实现的是
Thread
对象具有
id、title、body、created\u at
属性,而
Post
对象具有
id、body、created\u at
属性


现在根据当前场景,一个
线程
,可能有多个
帖子
。在这里,您将始终在对象
线程
e,g
Thread::with('post')->get()的关系中获得
body
created_,反之亦然

好的,在对您的问题进行了一些研究之后,这里是我的最新答案。正如您定义的表模式,即

threads
   id - integer
   title - string
posts
   id - integer
   body - text
   created_at - timestamp
   thread_id - integer (fk)
这意味着
Thread
有多个或只有一个(取决于关系)
Post
,而
Post
属于
Thread
,因此关系如下

class Thread extends Model{ 
   protected $table = 'threads';
   public function post()
   {
      return $this->hasMany('App\Post');//Or hasOne('App\Post')
   } 
}
class Post extends Model { 
   public function thread()
   {
       return $this->belongsTo('App\Thread');
   }  
}
您想要实现的是
Thread
对象具有
id、title、body、created\u at
属性,而
Post
对象具有
id、body、created\u at
属性


现在根据当前场景,一个
线程
,可能有多个
帖子
。在这里,您将始终在对象
线程
e,g
Thread::with('post')->get()的关系中获得
body
created_
,反之亦然

您是如何定义关系的?这看起来像是MySQL错误-该列是否真的存在于数据库中?另外,向我们展示生成错误的代码。如何在代码中访问模型的字段?@Don'tPanic是的,这是MySQL错误。它在threads表中查找body字段,显然不存在…@GauravRai刚刚添加了relationshipsHow您已经定义了Relationship?这看起来像是MySQL错误-该列真的存在于数据库中吗?另外,向我们展示生成错误的代码。如何在代码中访问模型的字段?@Don'tPanic是的,这是MySQL错误。它在threads表中查找body字段,显然不在那里…@GauravRai刚刚添加了relationship我相信是的,因为
threads
表中表示的
Thread
对象如果没有
Post
对象属性就没有意义。我相信是的,因为如果没有
Post
对象属性,由
threads
表表示的
Thread
对象就没有意义。这并不能真正回答我的问题。这只是一个解决办法,可以获得
线程的完整数据,同时仍然只是
Post
实例。这并不能真正回答我的问题。这只是一种解决方法,可以获得
线程的完整数据,同时仍然只是
Post
实例。