首页 文章

Laravel - 从created_at(不相关)排序的几个表中获取数据[重复]

提问于
浏览
2

这个问题与以下内容完全相同:

我想从几个表(不相关)中获取数据,并通过created_at列进行排序 .

例如,我有表:

posts: id Headers 文本created_at

products: id名称描述created_at

users: id name email created_at

如您所见,在每个表上都有 created_at ,我希望在一个查询中通过created_at对这些表中的数据进行排序 . 它将类似于Facebook上的墙 .

1 回答

  • 0

    正如一些人评论的那样,你不能在一个查询中做到这一点 .

    请改用 Collection .

    $posts = App\Post::all();
    $products = App\Product::all();
    $users = App\User::all();
    
    $collection = new \Illuminate\Support\Collection();
    $collection = $collection->merge($posts)
                             ->merge($products)
                             ->merge($users)
                             ->sortBy('created_at');
    

    然后使用 $collection 对象显示数据 .

相关问题