【问题标题】:Relationship between 4 tables Laravel 5.14张表之间的关系Laravel 5.1
【发布时间】:2016-04-25 00:05:43
【问题描述】:

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

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');
}

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

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

【问题讨论】:

  • 你不能在单个查询中使用 eloquent.. 但你可以在 4 个查询中做到这一点
  • 我正在为移动 API 工作,如果我使用 4 个查询会太慢
  • 它可能比使用连接的 1 个长查询更快。如果您想使用连接,则可以轻松测试它,而不是在这里查看:laravel.com/docs/5.1/queries#joins
  • 你能显示你的控制器代码来获取计数吗?

标签: php mysql eloquent laravel-5.1 relationship


【解决方案1】:

您可以通过定义的函数轻松访问所有这些属性。

例子:

$feed = Feed::find($id);

foreach($feed->likes() as $like){
    echo $like->user()->get('username');
}

等等。直到你调用 ->get() 时,你正在访问一个对象,它可以遍历所有这些。

【讨论】:

    【解决方案2】:

    试试这个

    $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);
    

    【讨论】:

      猜你喜欢
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 2015-08-02
      相关资源
      最近更新 更多