Adding linked table data to query builder

  laravel, mysql, php

I’m still getting comfortable with Laravel and am really struggling with my current project…

I’m trying to implement "reposts" on my social media feed that uses Laravel for backend. I have a table for posts and a table for reposts, which contains post_id (foreign key linking to posts), user_id (who reposted), created_at (when post was reposted).

I have a query builder to find posts and a query builder to find reposts and I’d like to make a union between the two. The part I’m struggling, however, is bringing over user_id and created_at (which live on the reposts table) when I attempt the following query on the posts table:

DB::table(‘posts’)→wherein(‘id’, ‘repost_ids’)→get();

How could I achieve this? And how could I make sure there are no duplicates when I execute the union?

Thank you in advance!

Source: Laravel

Leave a Reply