You want to implement single table inheritance to create multiple model types that share common properties yet have unique ones.
Consider using single table inheritance. In this approach, you have:
Single table inheritance can lead to null values. Consider multi table inheritance instead:
Post
<code class="php">class Post extends Eloquent { public function postable() { return $this->morphTo(); } }</code>
Question / Article
<code class="php">class Question extends Post { public function post() { return $this->morphOne('Post', 'postable'); } }</code>
Usage:
<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>
The above is the detailed content of Here are a few title options that focus on the question and answer format, using a question format: **Option 1 (Direct and Clear):** * **Single Table Inheritance vs. Multi Table Inheritance: Which i. For more information, please follow other related articles on the PHP Chinese website!