php - 从 Eloquent 集合中删除不匹配的条件记录

标签 php laravel laravel-5 eloquent eloquent-relationship

我在使用 Eloquent Eager Loading 时遇到了一些问题。我添加了 whereHas 以删除不符合评论标准的博客,但评论仍然返回空数组。我的意图是将其从 json 记录中完全删除。

如何彻底去除不符合我条件的json数据链?

我当前的代码:

User::select("id", "name", "email")
    ->with(['blog', 'blog.author', 'blog.comments' => function ($query) {
        $query->where('comment', 'John is here');
    }, 'blog.comments.owner'])
    ->whereHas('blog.comments', function ($query) {
        $query->where('comment', 'John is Here');
    })
    ->get();

我当前的 json 输出是:

{
    "id": 1,
    "name": "John Smith",
    "email": "john.smith@hotmail.com",
    "blog": [
        {
            "id": 1,
            "created_at": "2021-04-09T18:08:06.000000Z",
            "updated_at": "2021-04-09T10:33:03.000000Z",
            "title": "First Blog",
            "description": "Awesome",
            "users_id": 1,
            "cover": null,
            "author": {
                "id": 1,
                "name": "John Smith",
                "email": "john.smith@hotmail.com",
                "email_verified_at": null,
                "created_at": "2021-04-08T13:29:13.000000Z",
                "updated_at": "2021-04-08T13:29:13.000000Z",
                "role": 0
            },
            "comments": [
                {
                    "id": 1,
                    "comment": "John is here",
                    "blog_id": 1,
                    "user_id": 1,
                    "created_at": null,
                    "updated_at": null,
                    "owner": {
                        "id": 1,
                        "name": "John Smith",
                        "email": "john.smith@hotmail.com",
                        "email_verified_at": null,
                        "created_at": "2021-04-08T13:29:13.000000Z",
                        "updated_at": "2021-04-08T13:29:13.000000Z",
                        "role": 0
                    }
                }
            ]
        },
        {
            "id": 6,
            "created_at": "2021-04-12T07:41:43.000000Z",
            "updated_at": "2021-04-12T08:01:18.000000Z",
            "title": "Second Blog",
            "description": "Awesome",
            "users_id": 1,
            "cover": "images/json_1618213303.png",
            "author": {
                "id": 1,
                "name": "John Smith",
                "email": "john.smith@hotmail.com",
                "email_verified_at": null,
                "created_at": "2021-04-08T13:29:13.000000Z",
                "updated_at": "2021-04-08T13:29:13.000000Z",
                "role": 0
            },
            "comments": []
        }
    ]
}

我的预期输出是:

{
    "id": 1,
    "name": "John Smith",
    "email": "john.smith@hotmail.com",
    "blog": [
        {
            "id": 1,
            "created_at": "2021-04-09T18:08:06.000000Z",
            "updated_at": "2021-04-09T10:33:03.000000Z",
            "title": "First Blog",
            "description": "Awesome",
            "users_id": 1,
            "cover": null,
            "author": {
                "id": 1,
                "name": "John Smith",
                "email": "john.smith@hotmail.com",
                "email_verified_at": null,
                "created_at": "2021-04-08T13:29:13.000000Z",
                "updated_at": "2021-04-08T13:29:13.000000Z",
                "role": 0
            },
            "comments": [
                {
                    "id": 1,
                    "comment": "John is here",
                    "blog_id": 1,
                    "user_id": 1,
                    "created_at": null,
                    "updated_at": null,
                    "owner": {
                        "id": 1,
                        "name": "John Smith",
                        "email": "john.smith@hotmail.com",
                        "email_verified_at": null,
                        "created_at": "2021-04-08T13:29:13.000000Z",
                        "updated_at": "2021-04-08T13:29:13.000000Z",
                        "role": 0
                    }
                }
            ]
        }
    ]
}

最佳答案

试试这个,

User::select("id", "name", "email")
    ->with([
        'blog' => function ($query) {
            $query->whereHas('comments', function ($query) {
                $query->where('comment', 'John is Here');
            });
        }, 
        'blog.comments' => function ($query) {
            $query->where('comment', 'John is here');
        },
        'blog.author',  
        'blog.comments.owner'])
    ->get();

您也应该对 blog 预加载应用约束。您的查询仅过滤 blog

comments

关于php - 从 Eloquent 集合中删除不匹配的条件记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67191202/

相关文章:

php - "Method brand does not exist."

php - Laravel:如何在表单请求验证中使用忽略规则

laravel - laravel 5.3 当页数 = 1 时的分页

database - 计算 Laravel 5 每次页面加载中的查询数

php - 如何测试使用字符串正文发送的 Laravel 邮件,而不是使用 Mailable

php - 在 MySQL 中编辑记录时检查重复项

Laravel where日期和地点

php - oauth-private.key 不存在或不可读

php - 来自 Chrome 的混合安全/不安全消息

php - Yii - 模型和 Controller 之间的界限 - 什么方法在哪里? MVC原则