Aaron
Aaron

Reputation: 83

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 :

$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 ?

Upvotes: 0

Views: 1907

Answers (3)

Aaron
Aaron

Reputation: 83

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;
        });

Upvotes: 3

d3jn
d3jn

Reputation: 1410

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).

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;
    });

Upvotes: 0

Jerodev
Jerodev

Reputation: 33186

You need to use the map() function to loop over the package_details as well. The each function won't return or change anything unless you pass the value by reference.

$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;
    });

Upvotes: 0

Related Questions