Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chriz74's avatar

How to modify a value in a collection based on condition?

The question is simple, how do we modify an element in a collection? I have a collection of items and one key is for the picture. The picture data can be an external link or stored locally. So what I am doing is checking with this function if the picture data exists on the remote server:

    function checkRemoteFile($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        // don't download content
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if(curl_exec($ch)!==FALSE)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

What I need to do is, if the result is true it means that the picture exists remotely. If it's false it means it doesn't exist remotely. In this case I want to replace the name of the picture to a "image-not-found.png" which I store locally. The question is how do you modify that data without looping? Is it possible?

0 likes
8 replies
cre3z's avatar

is there a reason you do not want to use looping? This would guarantee you the result you need.

I think what you are looking for is the map function for your collection:

https://laravel.com/docs/5.5/collections#method-map

You loop through the collection and changing what you need.

$collection = collect([1, 2, 3, 4, 5]);

$multiplied = $collection->map(function ($item, $key) {
        return $item * 2;
    //in your case you will check and set your image source here
});

$multiplied->all();

// [2, 4, 6, 8, 10]

The other option would be to attach a method to your model that formats the data when you retrieve it.

chriz74's avatar

I'm trying a solution with loop I will post it here. You tell me what you think.

chriz74's avatar

here:

/** check picture data */
foreach ($models as $model) {

    /** picture data must be present */
    if ($model->picture != NULL) {

        /** path to locally stored picture */
        $image_path = public_path('images/models/' . $model->picture . '');

        /** is the file in storage? */
        if (File::exists($image_path)) {

            $model->picture = 'images/models/' . $model->picture . '';

        } else {

            /** if not in storage is it a link to a remote server picture? */
            if (!$this->checkRemoteFile($model->picture)) {

                $model->picture = '/images/errors/image-not-found.png';

            }

        }

    }else{

        $model->picture = '/images/errors/image-not-found.png';

    }

}

There is a little problem with this though. Before showing results this loops check online for every picture and it takes forever so I need to find another way to check if the online picture exists, maybe I can catch 404 on the blade view ?

topvillas's avatar

Use a queue worker when the model is created and populate the picture column.

chriz74's avatar

I don't understand. When the model is created? what for? The picture column is already populated.

cre3z's avatar

Well for large data sets this will happen yes, its a long process.

If I were you I would just write a script that checks the images once a day and pushes them into your database. So that when you retrieve the record you do not need to check for the images.

Snapey's avatar

Why fetch models that have a null picture element? Add that into the where clause for the model so that you won't have any null entries to start with. That will remove one if statement.

Please or to participate in this conversation.