Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

php - append url for JSON array in laravel

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(),

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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']),
    ];
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...