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.