简体   繁体   English

我想在 Laravel 集合中返回一些键和值

[英]I want to return some key and value in the Laravel collection

I have two Models (Store, Product) and Relationship hasMany我有两个模型(商店、产品)和关系 hasMany

public function products(){
   return $this->hasMany(Product::class);
}

And I want to return response collection, in class StoresCollection extends ResourceCollection我想返回响应集合,在 class StoresCollection extends ResourceCollection

public function toArray($request)
    {
        return $this->collection->map(function ($item) {
            return [
                'id' => $item->id,
                'seller_id' => $item->seller_id,
                'store_product' =>  $item->products()->get(),
            ];
        });
    }

But I don't want to return every key in the "store_product", I just need "id" and "is_featured" only, and I don't want them all.但我不想返回“store_product”中的每个键,我只需要“id”和“is_featured”,我不想要它们。

{
    "status": "success",
    "message": [],
    "code": 200,
    "data": [
        {
            "id": 5,
            "seller_id": 6,
            "store_product": [
                {
                    "id": 1017,
                    "seller_id": 89,
                    "is_featured": 0,
                    "is_category_featured": 0,
                    "is_approved": 1,
                    "created_at": "2020-4-21T00:00:00.000000Z",
                    "updated_at": "2020-4-21T00:00:00.000000Z"
                }
            ]
        },
        {
            "id": 5,
            "seller_id": 6,
            "store_product": [
                {
                    "id": 1018,
                    "seller_id": 89,
                    "is_featured": 0,
                    "is_category_featured": 0,
                    "is_approved": 1,
                    "created_at": "2020-4-21T00:00:00.000000Z",
                    "updated_at": "2020-4-21T00:00:00.000000Z"
                }
            ]
        },
    "paging": {
        "total": 2,
        "per_page": 15,
        "current_page": 1,
        "last_page": 1,
        "from": 1,
        "to": 2
    }
}

It's possible to request only some columns by get() method, in your case like:在您的情况下,可以通过get()方法仅请求某些列,例如:

'store_product' =>  $item->products()->get(['id', 'is_featured']),

Create a Resource for store_product eg StoreProductResource and define the keys in the toArray() method.为 store_product 创建一个资源,例如StoreProductResource并在 toArray() 方法中定义键。

Then come back and modify StoreCollection::toArray() like this:然后回来修改 StoreCollection::toArray() 如下:

 return $this->collection->map(function ($item) {
            return [
                'id' => $item->id,
                'seller_id' => $item->seller_id,

                'store_product' =>  StoreProductResource::collection($item->products()->get()),
            ];
        });

I also believe the callback map is unnecessary in your use case.我也相信在您的用例中不需要回调 map。 toArray() just needs to return an array. toArray() 只需要返回一个数组。 Laravel handles the mapping. Laravel 处理映射。 Except there's some logic you're doing in the callback which isn't included in the same code.除了您在回调中执行的某些逻辑未包含在同一代码中。

You can define your keys that you want to return in relationship as well:您也可以定义要在关系中返回的键:

public function products(){
   return $this->hasMany(Product::class)->select(['store_id','id', 'is_featured']);
}

Note: Remember to add the columns assigned to the foreign key matching both tables.注意:请记住添加分配给匹配两个表的外键的列。 For instance, in my example, I assumed a Store has a Product , meaning that the columns assigned to the foreign key would be something like store.id = product.store_id , so I had to add store_id to the list of selected columns;例如,在我的示例中,我假设Store具有Product ,这意味着分配给外键的列将类似于store.id = product.store_id ,因此我必须将store_id添加到选定列的列表中;

This is very simple to implement this one实现这一点非常简单

'store_product' =>  $item->products()->value('id', 'is_featured'),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM