Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

timgavin's avatar

Retweet/Reblog Setup

I'm trying to figure out a good way to reblog/retweet posts using eloquent instead of using the query builder. I have relationships set up and am able to reblog a post - storing the post_id and user_id in a pivot table, however, I'm having an issue when retrieving them; I want the post and the reblog to show in the user's stream together, and the way I have it set up is that the reblog is a relationship of the post, so I'm not sure how to retrieve the original post contents. If that's even possible?

Hopefully this makes sense!

Here are my tables, truncated for brevity.

Schema::create('posts', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id')->unsigned()->index();
    $table->string('title', 60)->nullable();
    $table->text('body')->nullable();
});

Schema::create('post_reblog', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('post_id')->unsigned();
    $table->integer('user_id')->unsigned();
});

And my relationships

class Post extends Model
{
    public function reblogs()
    {
        return $this->hasMany(Reblog::class);
    }
}

class Reblog extends Model
{
    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

I want it to display in the stream as

POST 1 (by user 1)
Title: Hello, World!
Body: Hi world, what's shakin'?
POST 2 (by user 1)
Title: I'm Post Two!
Body: Lorem Ipsum...
POST 1 (reblogged by user 2)
Title: Hello, World!
Body: Hi world, what's shakin'?
POST 3 ...
Title: I'm another post!
Body: I'm one fine body.
0 likes
1 reply
timgavin's avatar

@ usama.ashraf I took your advice and it worked out perfectly. Thanks :)

Please or to participate in this conversation.