0

I'm working with a Laravel application where I need to manage multiple image uploads for an existing project. I'm trying to update the image list by first deleting old images from storage, then adding the paths of these old images to a new array along with the paths of newly uploaded images. However, when I upload new images and attempt to update the array that holds the image paths, I encounter an issue where the existing image paths return as an empty string in the format [{}, "new_image_path"].

Additionally, I've tried using json_decode on the array, but I get an error saying json_decode(): Argument #1 ($json) must be of type string, array given.

$pricelists = [];
$pricelistcount = 1;
if ($request->hasFile('project_pricelist_url')) {

    $existingPricelists = $project->project_pricelist_url ?: [];
        foreach ($existingPricelists as $existingPricelist) {
            Storage::delete('public/project/pricelists/' . $existingPricelist);
        }

        foreach ($existingPricelists as $existingFilename) {
            $pricelists[] = $existingFilename; // Add existing filenames to the new array
        }





    foreach ($request->file('project_pricelist_url') as $file) {
        $currentDateTime = now()->format('YmdHis');
        $filename = $initials . "_" . $pricelistcount . '_' . $currentDateTime . '.' . $file->getClientOriginalExtension(); // Buat nama unik untuk setiap file
        $file->storeAs('public/project/pricelists', $filename); // Simpan file ke direktori yang diinginkan
        $pricelistcount++;
        $pricelists[] = $filename; // Simpan nama file dalam array
    }
    $project->project_pricelist_url =  json_encode($pricelists);

}

How can I correctly manage and update the list of image paths during the upload process to ensure both old and new image paths are retained and formatted correctly?

3
  • Can you explain more what you want to do ? Commented Jul 10, 2024 at 11:01
  • @xenooooo i want to append new image path instead of just replacing all existing images Commented Jul 12, 2024 at 8:03
  • Do you store the image paths inside the database in one column ? Commented Jul 13, 2024 at 11:42

1 Answer 1

0

complete ternary operator have only else part,

try this below code, hope this helps

if ($request->hasFile('project_pricelist_url')) {
   $existingPricelists = !empty($project->project_pricelist_url) ? $project->project_pricelist_url : [];
   $pricelists = [];

    foreach ($existingPricelists as $existingPricelist) {
        $existingFilename = $existingPricelist;
        $pricelists[] = $existingFilename; // Add existing filenames to the new array
        Storage::delete('public/project/pricelists/' . $existingPricelist);
    }

    // remove second loop

    $files = $request->file('project_pricelist_url');
    foreach ($files as $file) {
        $currentDateTime = now()->format('YmdHis');
        $filename = $initials . "_" . $pricelistcount . '_' . $currentDateTime . '.' . $file->getClientOriginalExtension(); // Buat nama unik untuk setiap file
        $file->storeAs('public/project/pricelists', $filename); // Simpan file ke direktori yang diinginkan
        $pricelistcount++;

        $pricelists[] = $filename; // Simpan nama file dalam array
    }
    
    $project->project_pricelist_url = json_encode($pricelists);
}
Sign up to request clarification or add additional context in comments.

2 Comments

the result i get in column is [{}, "rg_1_20240712150824.jpg"] and second when i change from StoreAs to move, the file doesnt even being stored, and couldnt find the image.
@MartinusGoh removed second loop, and changed move() to storeAs() as you said move() is not working in your case. Also check old images are deleting ?

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.