简体   繁体   English

覆盖嵌套在 Laravel 上的值集合

[英]Overwrite value collection nested on laravel

I've a nested collection like this :我有一个这样的嵌套集合:

"package_detail": [
                {
                    "id": 229,
                    "package_id": 66,
                    "data_scoin_id": 210,
                    "unit_scoin_id": 3,
                    "created_at": "2020-01-16 21:51:18",
                    "updated_at": null,
                    "created_by": 1,
                    "updated_by": null,
                    "unit_scoin": {
                        "id": 3,
                        "unit_scoin": 1000,
                        "code_scoin": "SCOINAKHDLL2019                                                                                                                                                                                                                                                ",
                        "description": "Seribu Scoin",
                        "rate_exchange": "Rp. 1.000",
                        "created_at": "2019-09-07 10:52:47",
                        "updated_at": null,
                        "created_by": null,
                        "updated_by": null
                    }
                },

I want to overwrite unit_scoin (parent) with unit_scoin (child) I've been try to map and each the the package_detail index like this :我想用 unit_scoin (child) 覆盖 unit_scoin (parent) 我一直在尝试映射每个package_detail索引,如下所示:

$packages = ScoinPackage::with('package_detail.unit_scoin')->get()
        ->map(function($d){
            $d->package_detail->each(function($dd) use (&$arr){
                $dd->unit_scoin = $dd->unit_scoin->unit_scoin;
            });

            return $d;
        });

But it was doesn't work, anyone can help me out ?但它不起作用,任何人都可以帮助我吗?

SOLVED解决了

conflict cause the object name is same.So first, save in a variable and unset it冲突导致对象名称相同。所以首先,保存在一个变量中并取消它

$packages = ScoinPackage::with('package_detail.unit_scoin')->get()->map(function($d){
            $d->package_detail->map(function($dd){
                $unit = $dd->unit_scoin;
                unset($dd->unit_scoin); 
                $dd->unit_scoin = $unit->unit_scoin;
                return $dd;
            });
            return $d;
        });

You need to use the map() function to loop over the package_details as well.您还需要使用map()函数来循环package_details The each function won't return or change anything unless you pass the value by reference.除非您通过引用传递值,否则 each 函数不会返回或更改任何内容。

$packages = ScoinPackage::with('package_detail.unit_scoin')->get()
    ->map(function($d){
        $d->package_detail->map(function($dd) use (&$arr){
            $dd->unit_scoin = $dd->unit_scoin->unit_scoin;

            return $dd;
        });

        return $d;
    });

You need to use transform() method, since each() just iterates over the collection without changing and map() one creates and returns new collection object (and doesn't modify initial one).您需要使用transform()方法,因为each()只是在不更改的情况下迭代集合,而map()创建并返回新的集合对象(并且不会修改初始对象)。

Something like this should do a trick:像这样的事情应该有一个技巧:

$packages = ScoinPackage::with('package_detail.unit_scoin')->get()
    ->map(function($d){
        $d->package_detail->transform(function ($dd) {
            return $dd->unit_scoin = $dd->unit_scoin->unit_scoin;
        });

        return $d;
    });

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

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