How can I implement single table inheritance using Laravel's Eloquent?

0 votes

Currently I have a model class named Post.

class Post extends Eloquent {

    protected $table = 'posts';
    protected $fillable = array('user_id', 'title', 'description', 'views');

    /*
     * Relationships
     */

    public function user()
    {
        return $this->belongsTo('User');
    }

    public function tags()
    {
        return $this->belongsToMany('Tag', 'post_tags');
    }

    public function reactions()
    {
        return $this->hasMany('Reaction');
    }

    public function votes()
    {
        return $this->hasMany('PostVote');
    }

    //Scopes and functions...
}

What is the best way to do this and where to start?

Oct 23 in Laravel by kartik
• 35,300 points
41 views

1 answer to this question.

0 votes

Hello @kartik,

Global Scope is available:

class Article extends Post
{
    protected $table = 'posts';

    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope('article', function (Builder $builder) {
            $builder->where('type', 'article');
        });

        static::creating(function ($article) {
            $article->type = 'article' 
        }); 
    }
}

where type = 'article' is added to all query of Article just like SoftDeletes.

>>> App\Article::where('id', 1)->toSql()
=> "select * from `posts` where `id` = ? and `type` = ?"

Actually laravel provide SoftDeletes trait using this feature.

Hope it helps!!

answered Oct 23 by Niroj
• 76,540 points

Related Questions In Laravel

0 votes
1 answer

How can I echo the version of the current Laravel version in php using the view?

Hello @kartik, This is the way how to ...READ MORE

answered Aug 10 in Laravel by Niroj
• 76,540 points
122 views
0 votes
1 answer

How can I query raw via Eloquent?

Hello @kartik, You may try this: // query can't ...READ MORE

answered Sep 24 in Laravel by Niroj
• 76,540 points
50 views
0 votes
1 answer

How to update a pivot table using Eloquent in laravel 5?

Hello @kartik, The code below solved my problem: $messages ...READ MORE

answered Sep 24 in Laravel by Niroj
• 76,540 points
218 views
0 votes
1 answer

How do I get a “select count(*) group by” using laravel eloquent?

Hello @kartik, You could use this: $reserves = DB::table('reserves')->selectRaw('*, ...READ MORE

answered Oct 21 in Laravel by Niroj
• 76,540 points
167 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27 in Java-Script by Niroj
• 76,540 points
437 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5 in Java-Script by Niroj
• 76,540 points
670 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8 in PHP by Niroj
• 76,540 points
88 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8 in Node-js by Niroj
• 76,540 points
87 views
0 votes
1 answer

How can we create a record in Laravel using eloquent?

We need to create a new model ...READ MORE

answered Mar 20 in Laravel by Niroj
• 76,540 points
137 views
0 votes
1 answer

How can we Create Multiple Where Clause Query Using Laravel Eloquent?

Hii, You can use Conditions using Array: $users = User::where([ ...READ MORE

answered Mar 30 in Laravel by Niroj
• 76,540 points
3,705 views