In my Laravel application, I have three database tables with exact same structure/schema and then there is a 'users' table containing all users.
All three tables have the following structure...
table1
id user_id description updated_at created_at
1 1 This is nice xxxxxxxxxx xxxxxxxxx
2 2 yes right it is xxxxxxxxxx xxxxxxxxx
table2
id user_id description updated_at created_at
1 3 Another text xxxxxxxxxx xxxxxxxxx
2 4 And yet more xxxxxxxxxx xxxxxxxxx
table3
id user_id description updated_at created_at
1 5 More nice xxxxxxxxxx xxxxxxxxx
2 6 okay done xxxxxxxxxx xxxxxxxxx
Now I really want to keep data in these separate tables. However, there is one odd times where I would need to show all these entries from these three tables in a same view, preferably orderedBy created_at field.
I used the following code to union these tables:
$table2 = DB::table('table2');
$table3 = DB::table('table3');
$query = DB::table('table1')
->union($table1)
->union($table3)
->get();
The problem that arises is that the eloquent relationship doesn't work and this statement in Blade breaks. {{$comment->user->name}}
.
I just want to be able to union/merge all these three tables and preferably be able to have the relationships or find some other way so that I could get the name of the user who owns a particular entry in the union/merged result. And also have the union/merged result orderedBy created_at column.
Any help would be greatly appreciated.
Thanks