过滤集合,以删除有空数组的项目。

问题描述 投票:0回答:1

我有一个收藏,看起来像这样。

[
{
    "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);
       }
    });

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

php laravel eloquent laravel-collection
1个回答
0
投票

你在循环中返回,所以一旦发现没有资产的listing,它就会打破循环,成为false。相反,使用 contains(),检查是否有任何元素,通过给定的条件。如果有,它将返回true。这样就不会像现在这样,在空集合上中断循环。

return $category->filter(function($item){
   return $item->listings->contains(function ($listing) {
       return $listing->assets->isNotEmpty();
   });
});
热门问题
推荐问题
最新问题