您想要实现单表继承来创建多个模型类型,这些模型类型共享公共属性但又具有唯一属性.
考虑使用单表继承。在这种方法中,您有:
单表继承可能会导致空值。请考虑多表继承:
Post
<code class="php">class Post extends Eloquent { public function postable() { return $this->morphTo(); } }</code>
问题 / 文章
<code class="php">class Question extends Post { public function post() { return $this->morphOne('Post', 'postable'); } }</code>
用法:
<code class="php">$posts = Post::all(); $questions = Question::all(); $post = Post::find(1); $question_column2 = $post->postable->question_column2; $shared_column = $question->post->shared_column;</code>
以上是以下是一些专注于问题和答案格式的标题选项,使用问题格式: **选项 1(直接且清晰):** * **单表继承与多表继承:哪个我的详细内容。更多信息请关注PHP中文网其他相关文章!