通过匹配集合值附加新的键和值

问题描述

SO 伟大的人们你好!

首先,如果我的英语不是很好,我很抱歉,但我会尽力在这里描述我的问题

我有 3 个模型

  1. 用户
  2. 发布
  3. 喜欢

用户.PHP

___________________________________________________________
| id  | name | email | password | created_at | updated_at |
| ... | ...  | ...   | ...      | ...        | ...        |
| ... | ...  | ...   | ...      | ...        | ...        |
| ... | ...  | ...   | ...      | ...        | ...        |

User model relationship:

public function posts() {
    return $this->hasMany(Post::class,'user_id','id');
}

Post.PHP

__________________________________________________
| id  | user_id | body | created_at | updated_at |
| ... | ...     | ...  | ...        | ...        |
| ... | ...     | ...  | ...        | ...        |

Post model relationship:

public function user() {
    return $this->belongsTo(User::class,'id');
}

public function likes() {
    return $this->morphMany('likeable');
}

喜欢.PHP

_________________________________________________________________________
| id  | likeable_type | likeable_id | user_id | created_at | updated_at |
| ... | ...           | ...         | ...     | ...        | ...        |
| ... | ...           | ...         | ...     | ...        | ...        |
| ... | ...           | ...         | ...     | ...        | ...        |

Like model relationship:

public function likeable() {
    return $this->morphTo('likeable');
}

一切正常,对于简单的 C.R.U.D

问题出现在我使用 Laravel Debugbar 时, 我看到很多重复的查询只是为了获取一些记录:

Ex:
    // Let say that I have 5 users
    $users = User::all();

    foreach ($users as $user) {
        $user->load('posts');
    }

    return $users;

    // Result
    select * from `posts`.`user_id` 1 ...
    select * from `posts`.`user_id` 2 ...
    select * from `posts`.`user_id` 3 ...

所以我决定改变方法

Ex:
    $users = User::all();

    $posts = Post::whereIn('user_id',$users->pluck('id')->toArray())->get();

    // Result:
    select * `posts`.`user_id` in (1,2,3,4,5)

我不再看到重复的查询,这很好, 解决这个重复查询后,我从特定帖子中获取“喜欢”

Ex:
    $users = User::all();

    $posts = Post::whereIn('user_id',$users->pluck('id')->toArray())->get();

    $posts_likes = Like::where('likeable_type','App\Post') // morphMany
                ->whereIn('likeable_id',$posts->pluck('id')->toArray())->get();
    

现在问题来了,我不知道如何将posts_likes与其帖子配对

Ex:
    $posts = [
        {
            'id': 1,'user_id': 2,'body': 'Lorem ipsum...',...
        },{
            'id': 2,'body': 'Sit amet...',{
            'id': 3,'user_id': 3,'body': 'abcde...',...    
        },... etc
    ];

    $posts_likes = [
        {
            'id': 1,'likeable_type': 'App\Post','likeable_id': 2,'id': 2,...
        {
            'id': 3,'likeable_id': 1,'user_id': 5,{
            'id': 4,'likeable_id': 3,'user_id': 1,... etc
    ];

我的问题:

如何通过匹配精确的 id 在 $posts 集合中插入喜欢? (post id == like likeable_id)

所以我可以循环访问它们,例如:$post->likes = [...]

Ex:
    $posts = [
        {
            'id': 1,'likes': [
                // All likes for post with this id (1)
            ],'likes': [
                // All likes for post with this id (2)
            ],...
    ];

如果有任何不清楚的解释,我会尽快编辑 提前致谢

解决方法

你可以全部加载

参考链接 https://laravel.com/docs/8.x/eloquent-relationships#nested-eager-loading

$users = User::with('posts.likes')->get();
return $users;

如果您设置正确的关系,此代码将起作用

//user model
public function posts()
{
    return $this->hasMany(Post::class,'user_id','id');
}

//post model
public function likes()
{
    return $this->morphMany('likeable');
}

我想生成json链接

[{
    "name": "user","posts": [{
            "name": "postName","likes": []
        },{
            "name": "postName","likes": []
        }
    ]
}]