2017-10-15 27 views
1

你好,所以我必须在Laravel中大量用户数据分布在多个模型存储在单独的表中的应用程序。我现在需要创建一个活动提要,这意味着按日期按表排列各种数据。Laravel雄辩合并查询活动供稿

为了便于说明,假设我有两个型号,CommentLike

我想要一个按日期组合的提要。 merge()不是一种选择,因为它们可能具有相同的id

因此我可以UNION他们,但我的问题是我不知道从什么什么来了。

我喜欢的表看起来像这样:

+------------+------------------+------+-----+---------+----------------+ 
| Field  | Type    | Null | Key | Default | Extra   | 
+------------+------------------+------+-----+---------+----------------+ 
| id   | int(10) unsigned | NO | PRI | NULL | auto_increment | 
| user_id | int(11)   | NO |  | NULL |    | 
| asset_id | int(11)   | NO |  | NULL |    | 
| created_at | timestamp  | YES |  | NULL |    | 
| updated_at | timestamp  | YES |  | NULL |    | 
+------------+------------------+------+-----+---------+----------------+ 

我的意见表看起来像这样:

+------------+------------------+------+-----+---------+----------------+ 
| Field  | Type    | Null | Key | Default | Extra   | 
+------------+------------------+------+-----+---------+----------------+ 
| id   | int(10) unsigned | NO | PRI | NULL | auto_increment | 
| asset_id | int(11)   | NO |  | NULL |    | 
| content | longtext   | NO |  | NULL |    | 
| user_id | int(11)   | NO |  | NULL |    | 
| created_at | timestamp  | YES |  | NULL |    | 
| updated_at | timestamp  | YES |  | NULL |    | 
+------------+------------------+------+-----+---------+----------------+ 

我的问题是,如果我只是联盟这两个查询,我只知道他们是不同的基于content列的存在,其可以在另一个模型中,例如Blurb或其他。

基本上,我怎么跨越车型多次查询,同时保持笔直属于什么地方,因为在我的活动饲料我想说的是,11分钟前你评价说,5分钟前你喜欢,等

我不想因为效率低而做多个查询,而且我也不想将所有活动(如喜欢和评论等)存储在一张表中。有没有我可以使用的别名,而不是重命名列我插入数据使用查询为查询的目的,因此例如一个评论选择将添加“评论”在临时字段,以便我可以访问它像

$data->type?我可以在所有表​​格中添加一个类型,但是我会占用空间不必要的,因为显然我知道如果评论表中的评论是评论表中的评论,那么这是我唯一的查询,但现在我正在重新思考我的观点结构给定我需要一个查询跨越多个表。

+0

有你喜欢的创建和评论之间的关系? – usrNotFound

+0

@usrNotFound不......我应该吗? –

+0

你怎么知道用户是否喜欢评论。我可以看到用户是,但你会如何处理评论? – usrNotFound

回答

1

你可以用下面的代码获取用户活动供稿。

$userId = Auth::id(); //or whatever you want. 
    $activity = DB::table('comment as ac') 
    ->select(DB::raw('ac.user_id , ac.asset_id , ac.comment , ac.created_at , ac.updated_at , "comment" as activity_type')) 
    ->where("ac.user_id", $userId) 
    ->union(
    DB::table('like as al') 
    ->select(DB::raw('al.user_id , al.asset_id , NULL as comment , al.created_at , al.updated_at , "like" as activity_type')) 
    ->where("al.user_id", $userId) 
    ) 
    ->latest() 
    ->get(); 
+1

感谢您有所帮助回答萨钦,我真的很感激它:) –

+0

欢迎您 - @AkshayPrabhakar –

1

执行查询时,根据表名选择一个附加原始值。例如,在原始的SQL:

SELECT likes.*, '' AS content, 'like' AS type 
FROM likes 
WHERE likes.user_id = 1 
UNION 
SELECT comments.*, 'comment' AS type 
FROM comments 
WHERE likes.user_id = 1 
ORDER BY created_at DESC 

的Laravel代码(未经测试)看起来像:

$activity = DB::table('comments') 
    ->select("comments.*, 'comment' AS type") 
    ->where('comments.user_id', $user->id) 
    ->union(
     DB::table('likes') 
      ->select("likes.*, '' AS content, 'like' AS type") 
      ->where('likes.user_id', $user->id) 
    ) 
    ->orderBy('created_at', 'ASC') 
    ->get();