我有3个表topic1,topic2,topic3。由于属性的巨大差异,我分为3个表。所有这些表都有一个common_timestamp
的公共列我想创建一个显示最新的时间轴,其中created_timestamp作为参考。
我已经尝试通过最大(t1.created_timestamp,t2.created_timestamp,t3.created_timestamp)加入表和顺序,但我不能在laravel的查询构建器中执行此操作。
另外,当显示属性时,它们将被重复,如果topic1是最新的,有没有办法只显示topic1属性,依此类推。
或者有更好的方法吗?
谢谢杰迪大师!
我忘了提到第四个表(跟随者表),它只显示3个主题表的created_by。
topic1 {var1, var2, created_timestamp, user_id)
topic2 {var3, var2, var3, created_timestamp, teacher_id)
topic3 {var3, created_timestamp, visitor_id)
follower {follow_id, creator_id}
follow_id = user_id,teacher_id,visitor_id(表连接) 我需要只获得我关注的行(也就是follower.creator_id = $ me)
答案 0 :(得分:0)
我认为最好的方法是为每个表运行查询,然后合并结果。
$a = DB::table('topic1')->join('follower', 'folower.follow_id', '=', 'topic1.user_id')->where('folower.follower_id', $followed)->get();
$b = DB::table('topic2')->join('follower', 'folower.follow_id', '=', 'topic2.teacher_id')->where('folower.follower_id', $followed)->get();
$c = DB::table('topic3')->join('follower', 'folower.follow_id', '=', 'topic3.visitor_id')->where('folower.follower_id', $followed)->get();
$results = new Illuminate\Database\Eloquent\Collection;
$results = $results->merge($a)->merge($b)->merge($c)->sortBy('created_timestamp');