首页 > 解决方案 > 过滤集合以删除具有空数组的项目

问题描述

我有一个看起来像这样的集合,

[
{
    "id": 1,
    "title": "Cooking",
    "deleted_at": null,
    "created_at": null,
    "updated_at": null,
    "listings": [
        {
            "id": 6,
            "title": "Listing Number 1",
            "slug": "listing-number-1",
            "description": "Description",
            "booking_details": "Booking Details",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T22:35:56.000000Z",
            "updated_at": "2020-06-03T22:35:56.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 6
            },
            "assets": [],
            "primary_image": []
        },
        {
            "id": 7,
            "title": "Listing Number 1",
            "slug": "listing-number-1",
            "description": "Description",
            "booking_details": "Booking Details",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T22:36:10.000000Z",
            "updated_at": "2020-06-03T22:36:10.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 7
            },
            "assets": [],
            "primary_image": []
        },
        {
            "id": 8,
            "title": "Listing Number 1",
            "slug": "listing-number-1",
            "description": "Description",
            "booking_details": "Booking Details",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T22:36:26.000000Z",
            "updated_at": "2020-06-03T22:36:26.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 8
            },
            "assets": [],
            "primary_image": []
        },
        {
            "id": 9,
            "title": "Listing Number 1",
            "slug": "listing-number-1",
            "description": "Description",
            "booking_details": "Booking Details",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T22:36:42.000000Z",
            "updated_at": "2020-06-03T22:36:42.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 9
            },
            "assets": [],
            "primary_image": []
        },
        {
            "id": 10,
            "title": "Listing Number 1",
            "slug": "listing-number-1",
            "description": "Description",
            "booking_details": "Booking Details",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T22:37:40.000000Z",
            "updated_at": "2020-06-03T22:37:40.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 10
            },
            "assets": [],
            "primary_image": []
        },
        {
            "id": 11,
            "title": "Listing Number 2",
            "slug": "listing-number-2",
            "description": "Description 1",
            "booking_details": "Booking Details 1",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T22:46:31.000000Z",
            "updated_at": "2020-06-03T22:46:31.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 11
            },
            "assets": [
                {
                    "id": 3,
                    "type": "image",
                    "path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-lg.jpg",
                    "is_primary": 1,
                    "assetable_type": "App\\Listing",
                    "assetable_id": 11,
                    "user_id": 2,
                    "deleted_at": null,
                    "created_at": "2020-06-03T22:46:33.000000Z",
                    "updated_at": "2020-06-03T22:46:33.000000Z"
                },
                {
                    "id": 4,
                    "type": "image",
                    "path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-sm.jpg",
                    "is_primary": 1,
                    "assetable_type": "App\\Listing",
                    "assetable_id": 11,
                    "user_id": 2,
                    "deleted_at": null,
                    "created_at": "2020-06-03T22:46:33.000000Z",
                    "updated_at": "2020-06-03T22:46:33.000000Z"
                }
            ],
            "primary_image": [
                {
                    "id": 3,
                    "type": "image",
                    "path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-lg.jpg",
                    "is_primary": 1,
                    "assetable_type": "App\\Listing",
                    "assetable_id": 11,
                    "user_id": 2,
                    "deleted_at": null,
                    "created_at": "2020-06-03T22:46:33.000000Z",
                    "updated_at": "2020-06-03T22:46:33.000000Z"
                },
                {
                    "id": 4,
                    "type": "image",
                    "path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-2/primary-image-sm.jpg",
                    "is_primary": 1,
                    "assetable_type": "App\\Listing",
                    "assetable_id": 11,
                    "user_id": 2,
                    "deleted_at": null,
                    "created_at": "2020-06-03T22:46:33.000000Z",
                    "updated_at": "2020-06-03T22:46:33.000000Z"
                }
            ]
        },
        {
            "id": 12,
            "title": "Listing Number 3",
            "slug": "listing-number-3",
            "description": "Description 1",
            "booking_details": "Booking Details 1",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T22:50:31.000000Z",
            "updated_at": "2020-06-03T22:50:31.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 12
            },
            "assets": [],
            "primary_image": []
        },
        {
            "id": 13,
            "title": "Listing Number 3",
            "slug": "listing-number-3",
            "description": "Description 1",
            "booking_details": "Booking Details 1",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T22:57:12.000000Z",
            "updated_at": "2020-06-03T22:57:12.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 13
            },
            "assets": [],
            "primary_image": []
        },
        {
            "id": 15,
            "title": "Listing Number 3",
            "slug": "listing-number-3",
            "description": "Description 1",
            "booking_details": "Booking Details 1",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T23:00:49.000000Z",
            "updated_at": "2020-06-03T23:00:49.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 15
            },
            "assets": [],
            "primary_image": []
        },
        {
            "id": 16,
            "title": "Listing Number 3",
            "slug": "listing-number-3",
            "description": "Description 1",
            "booking_details": "Booking Details 1",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T23:01:21.000000Z",
            "updated_at": "2020-06-03T23:01:21.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 16
            },
            "assets": [],
            "primary_image": []
        },
        {
            "id": 17,
            "title": "Listing Number 3",
            "slug": "listing-number-3",
            "description": "Description 1",
            "booking_details": "Booking Details 1",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T23:01:26.000000Z",
            "updated_at": "2020-06-03T23:01:26.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 17
            },
            "assets": [],
            "primary_image": []
        },
        {
            "id": 18,
            "title": "Listing Number 3",
            "slug": "listing-number-3",
            "description": "Description 1",
            "booking_details": "Booking Details 1",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T23:01:49.000000Z",
            "updated_at": "2020-06-03T23:01:49.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 18
            },
            "assets": [],
            "primary_image": []
        },
        {
            "id": 19,
            "title": "Listing Number 3",
            "slug": "listing-number-3",
            "description": "Description 1",
            "booking_details": "Booking Details 1",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T23:02:19.000000Z",
            "updated_at": "2020-06-03T23:02:19.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 19
            },
            "assets": [],
            "primary_image": []
        },
        {
            "id": 20,
            "title": "Listing Number 3",
            "slug": "listing-number-3",
            "description": "Description 1",
            "booking_details": "Booking Details 1",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T23:03:31.000000Z",
            "updated_at": "2020-06-03T23:03:31.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 20
            },
            "assets": [],
            "primary_image": []
        },
        {
            "id": 21,
            "title": "Listing Number 3",
            "slug": "listing-number-3",
            "description": "Description 1",
            "booking_details": "Booking Details 1",
            "cost": "25.00",
            "active": 0,
            "user_id": 2,
            "deleted_at": null,
            "created_at": "2020-06-03T23:06:25.000000Z",
            "updated_at": "2020-06-03T23:06:25.000000Z",
            "pivot": {
                "category_id": 1,
                "listing_id": 21
            },
            "assets": [
                {
                    "id": 5,
                    "type": "image",
                    "path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-lg.jpg",
                    "is_primary": 1,
                    "assetable_type": "App\\Listing",
                    "assetable_id": 21,
                    "user_id": 2,
                    "deleted_at": null,
                    "created_at": "2020-06-03T23:06:26.000000Z",
                    "updated_at": "2020-06-03T23:06:26.000000Z"
                },
                {
                    "id": 6,
                    "type": "image",
                    "path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-sm.jpg",
                    "is_primary": 1,
                    "assetable_type": "App\\Listing",
                    "assetable_id": 21,
                    "user_id": 2,
                    "deleted_at": null,
                    "created_at": "2020-06-03T23:06:27.000000Z",
                    "updated_at": "2020-06-03T23:06:27.000000Z"
                }
            ],
            "primary_image": [
                {
                    "id": 5,
                    "type": "image",
                    "path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-lg.jpg",
                    "is_primary": 1,
                    "assetable_type": "App\\Listing",
                    "assetable_id": 21,
                    "user_id": 2,
                    "deleted_at": null,
                    "created_at": "2020-06-03T23:06:26.000000Z",
                    "updated_at": "2020-06-03T23:06:26.000000Z"
                },
                {
                    "id": 6,
                    "type": "image",
                    "path": "https://xx-xxxx-xxxx-1.s3.eu-west-2.amazonaws.com/listing-number-3/primary-image-sm.jpg",
                    "is_primary": 1,
                    "assetable_type": "App\\Listing",
                    "assetable_id": 21,
                    "user_id": 2,
                    "deleted_at": null,
                    "created_at": "2020-06-03T23:06:27.000000Z",
                    "updated_at": "2020-06-03T23:06:27.000000Z"
                }
            ]
        }
    ]
}

]

我只想看到一个包含没有空资产数组的项目的集合,

我试过这个,

$category = Category::whereHas('listings.assets')->with(['listings.assets', 'listings.primaryImage' => function($query){
        $query->where('assets.is_primary', '=', 1);
    }])->get();
    return $category->filter(function($item){
       foreach($item->listings as $i) {
           return !empty($i->assets);
       }
    });

但这只是返回一个空的[]我做错了什么?

标签: phplaraveleloquentlaravel-collection

解决方案


您正在循环中返回,因此一旦找到没有资产的列表,它将打破循环并为假。而是使用contains(), 检查是否有任何元素通过给定条件。如果有,它将返回 true。因此,不要像现在这样打破空集合的循环。

return $category->filter(function($item){
   return $item->listings->contains(function ($listing) {
       return $listing->assets->isNotEmpty();
   });
});

推荐阅读