Laravel : Storage::Delete on Json Array

  Kiến thức lập trình
$projectphotos = [];
        $photocount = 1;
        if ($request->hasFile('project_photo_url')) {
            foreach ($request->file('project_photo_url') as $file) {
                $currentDateTime = now()->format('YmdHis');
                $filename = $initials . "_" . $photocount . '_' . $currentDateTime . '.' . $file->getClientOriginalExtension(); // Buat nama unik untuk setiap file
                $file->storeAs('public/project/photos', $filename); // Simpan file ke direktori yang diinginkan
                $photocount++;
                $projectphotos[] = $filename; // Simpan nama file dalam array
            }


            $projectPhotos = json_decode($project->project_photo_url);

            if (!empty($projectPhotos)) {
                // Menghapus file-file terkait dari penyimpanan
                foreach ($projectPhotos as $photo) {
                    Storage::delete('public/project/photos/' . $photo);
                }
            }
            $project->project_photo_url = json_encode($projectphotos);
        }


        $project->save();

my code is like this, but i got an error

json_decode(): Argument #1 ($json) must be of type string, array given

where my data is like this

["opot_1_20240419230832.png","opot_2_20240419230832.png"]

i want to delete existing data, but it keep adding new but ignore delete the old one

Tried not using json_decode, but it doesnt delete the existing

New contributor

Martinus Goh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT