php - 4 个表之间的关系 Laravel 5.1

标签 php mysql eloquent laravel-5.1 relationship

我需要显示带有总评论的提要,以及该提要上的总点赞数以及发表评论的用户的详细信息。

Feed 表

| id | movie_id | user_id  | description | 
|----|----------|----------|-------------|
| 1  | 1        | 1        | Lorem Ipsum |

评论表

| id | feed_id | user_id  | comment   | 
|----|-------- |----------|-----------|
| 1  | 1       | 2        | comment 1 |
| 2  | 1       | 3        | comment 2 |

点赞表

| id | feed_id | user_id  |
|----|-------- |----------|
| 1  | 1       | 2        |
| 2  | 1       | 3        |

用户表

| id | username| email  |
|----|-------- |--------|
| 1  | a       | a@a.com|
| 2  | b       | b@b.com|
| 3  | c       | c@c.com|

关系

Feed.php

public function user () {
    return $this->belongsTo('App\User');
}

public function likes () {
    return $this->hasMany('App\Like');
}

public function comments () {
    return $this->hasMany('App\Comment');
}

User.php

public function feeds () {
    return $this->belongsToMany('App\Feed');
}

public function like () {
    return $this->belongsTo('App\Like');
}

public function comment () {
    return $this->belongsTo('App\Comment');
}

Like.php

public function user () {
    return $this->belongsTo('App\User');
}

public function feed () {
    return $this->belongsTo('App\Feed');
}

Comment.php

public function user () {
    return $this->belongsTo('App\User');
}

public function feed () {
    return $this->belongsTo('App\Feed');
}

现在我需要获取所有提要(我已经完成了这个),包括评论数、点赞数和发表评论的用户详细信息。

我可以使用 Eloquent 在单个查询中获得它吗?

最佳答案

试试这个

$commentsCount = \App\Models\Comment::select('feed_id',\DB::raw('count(id) as comments_count'))->groupBy('feed_id')->toSql();
    $likesCount = \App\Models\Like::select('feed_id',\DB::raw('count(id) as likes_count'))->groupBy('feed_id')->toSql();
    $records = \DB::table('feeds as f')
            ->leftJoin('comments as c','f.id','=','c.feed_id')
            ->leftJoin('users as u','c.user_id','=','u.id')
            ->leftJoin(\DB::raw('('.$commentsCount.') as k'),'f.id','=','k.feed_id')
            ->leftJoin(\DB::raw('('.$likesCount.') as l'),'f.id','=','l.feed_id')
            ->select('f.id as fid','f.description','u.id as uid','u.name','u.email','k.comments_count','l.likes_count')
            ->orderBy('fid')
            ->get();

    $transform = function(array $records){
        $records = collect($records)->groupBy('fid');
        return $records->transform(function($items){
            $feed['id'] = $items->first()->fid;
            $feed['description'] = $items->first()->description;
            $feed['count'] = [
                'likes' => is_null($items->first()->likes_count) ? 0 : $items->first()->likes_count,
                'comments' => is_null($items->first()->comments_count) ? 0 : $items->first()->comments_count,
            ];
            $feed['users'] = $items->transform(function($user){
                return is_null($user->uid) ? [] : ['id'=>$user->uid,'name'=>$user->name,'email'=>$user->email];
            });

            return $feed;
        });
    };

    return array_values($transform($records)->toArray());

您可以将闭包函数与其他函数交换。喜欢

$this->transform($records);

关于php - 4 个表之间的关系 Laravel 5.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34878105/

相关文章:

php - 在 Laravel 中创建多态字段名的规则是什么?

PHP 关联数组上的全文搜索

php - 优化 MySQL 和 PHP 中的多个查询

mysql - 多对多表关系困境

mysql - 优化 Eloquent 关系检索

php - 添加到数据库时 Laravel float 变量更改值

javascript - 如何使用 jquery ajax 将数据传递到另一个页面

php - laravel find_in_set() sql 和 whereIn 的区别

javascript - 如何在 require() 之后更改所需的连接

mysql - 如何在 MySQL 中以字符串形式返回整数值