dsifjgogw48491752 2017-08-29 08:11
浏览 68

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

Assume we have two db tables: posts and threads:

threads
   id - integer
   title - string
posts
   id - integer
   body - text
   created_at - timestamp
   thread_id - integer (fk)

and two Eloquent models:

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

What I want to achieve is Thread object having id, title, body, created_at attributes, while Post object having id, body, created_at attributes.

Yet, I still get error:

Column not found: 1054 Unknown column 'body' in 'field list'

which is MySQL error that basically means that Laravel's trying to look up the body column in the threads table. However, it is stored in the posts table.

  • 写回答

2条回答 默认 最新

  • dtkf64283 2017-08-29 08:15
    关注

    What you are trying to do is unnecessary with Eloquent.

    The data that is retreived from the database iss storred in the $attributes property.

    So unless you are sharing any behaviour you don't need to inherit from anything other than Model.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度