0

I have a JSON field in my MySQL table column which has an JSON array with part of images URLs.

"images": [
            {
              "images": {
                 "original": "/storage/uploads/1.png",
                 "300": "/storage/uploads/300.1.png",
                 "600": "/storage/uploads/600.1.png",
                 "900": "/storage/uploads/900.1.png"
                    },
               "thumb": "/storage/uploads/300.1.png"
            },
            {
               "images": {
                  "original": "/storage/uploads/2.png",
                  "300": "/storage/uploads/300.2.png",
                  "600": "/storage/uploads/600.2.png",
                  "900": "/storage/uploads/900.2.png"
                    },
                "thumb": "/storage/uploads/300.2.png"
             },
             {},
           ]

I want to get the array with appending a Base URL for each of the values of the array.

    "images": [
            {
              "images": {
                 "original": "http://localhost:8000/storage/uploads/1.png",
                 "300": "http://localhost:8000/storage/uploads/300.1.png",
                 "600": "http://localhost:8000/storage/uploads/600.1.png",
                 "900": "http://localhost:8000/storage/uploads/900.1.png"
                    },
               "thumb": "http://localhost:8000/storage/uploads/300.1.png"
            },
            {
               "images": {
                  "original": "http://localhost:8000/storage/uploads/2.png",
                  "300": "http://localhost:8000/storage/uploads/300.2.png",
                  "600": "http://localhost:8000/storage/uploads/600.2.png",
                  "900": "http://localhost:8000/storage/uploads/900.2.png"
                    },
                "thumb": "http://localhost:8000/storage/uploads/300.2.png"
             },
             {},
           ]

I have tried with Collections function like below code. But was not succeeded.

'images' => collect($item->images)->map(function ($image) {
                return url($image);
             })->all(),
1
  • Do you have an idea for this question? @babak-ashrafi Commented Jan 7, 2021 at 12:07

2 Answers 2

1

For collections you would need to create a collection of the second images keyed array as well. collect() does not create a recursive collection.

So if you had the following array:

$item = [
    "images" => [
        [
            "images" => [
                "original" => "/storage/uploads/1.png",
                "300" => "/storage/uploads/300.1.png",
                "600" => "/storage/uploads/600.1.png",
                "900" => "/storage/uploads/900.1.png"
            ],
            "thumb" => "/storage/uploads/300.1.png",
        ], [
            "images" => [
                "original" => "/storage/uploads/2.png",
                "300" => "/storage/uploads/300.2.png",
                "600" => "/storage/uploads/600.2.png",
                "900" => "/storage/uploads/900.2.png",
            ],
            "thumb" => "/storage/uploads/300.2.png",
        ]
    ]
];

and you really wanted to use collection, then the following would work:

$item = collect($item['images'])->map(function ($item) {
    $images = collect($item['images'])->map(function ($item) {
        return url($item);
    });
  
    return [
        "images" => $images->toArray(),
        "thumb"  => url($item['thumb']),
    ];
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can use nested foreach to solve this

$json = json_decode($item->images, true); // I'm using true to convert the json to array

$images = [];

foreach ($json as $index => $array) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            foreach ($value as $path_key => $path) {
                $images[$index][$key][$path_key] = url($value[$path_key]);
            }
        } else {
            $images[$index][$key] = url($value);
        }
    }
}

If you dd($images); you will get

array:2 [▼
  0 => array:2 [▼
    "images" => array:4 [▼
      "original" => "http://localhost:8000/storage/uploads/1.png"
      300 => "http://localhost:8000/storage/uploads/300.1.png"
      600 => "http://localhost:8000/storage/uploads/600.1.png"
      900 => "http://localhost:8000/storage/uploads/900.1.png"
    ]
    "thumb" => "http://localhost:8000/storage/uploads/300.1.png"
  ]
  1 => array:2 [▼
    "images" => array:4 [▼
      "original" => "http://localhost:8000/storage/uploads/2.png"
      300 => "http://localhost:8000/storage/uploads/300.2.png"
      600 => "http://localhost:8000/storage/uploads/600.2.png"
      900 => "http://localhost:8000/storage/uploads/900.2.png"
    ]
    "thumb" => "http://localhost:8000/storage/uploads/300.2.png"
  ]
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.