In previous section we will see how to upload multiple image in laravel 9. For this section we will see how to edit update multiple image in laravel 9. Before we start you need to read below blog.
Laravel 9 Upload Multiple Images Tutorial Example
Now Add Edit and Update method.
app/Http/Controllers/projectController.php
public function edit(Project $project)
{
return view('projects.edit', compact('project'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Project $project
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Project $project)
{
$data = $request->validate([
'name' => 'required|string|max:255',
'images' => 'nullable|array'
]);
if ($request->hasFile('images')) {
$images = [];
foreach ($data['images'] as $image) {
\Storage::delete($project->images);
$fileName = uniqid() . '.' . $image->getClientOriginalExtension();
$image_path = $image->storeAs('images', $fileName, 'public');
array_push($images, $image_path);
$data['images'] = $images;
$project->update($data);
}
}
$project->update($data);
return redirect()->route('projects.index');
}
resources/views/projects/edit.blade.php
<x-app-layout>
<x-slot name="header">
<h2 class="text-xl font-semibold leading-tight text-gray-800">
{{ __('Project Edit') }}
</h2>
</x-slot>
<div class="py-12">
<div class="mx-auto max-w-7xl sm:px-6 lg:px-8">
<div class="overflow-hidden bg-white shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
<form method="POST" action="{{ route('projects.update',$project->id) }}"
enctype="multipart/form-data">
@csrf
@method('put')
<div class="mb-6">
<label class="block">
<span class="text-gray-700">Name</span>
<input type="text" name="name"
class="block w-full @error('name') border-red-500 @enderror mt-1 rounded-md"
placeholder="" value="{{old('name',$project->name)}}" />
</label>
@error('name')
<div class="text-sm text-red-600">{{ $message }}</div>
@enderror
</div>
<div class="mb-4">
<label class="block">
<span class="text-gray-700">Images</span>
<input type="file" name="images[]" class="block w-full mt-1 rounded-md" placeholder=""
multiple />
</label>
@error('images')
<div class="text-sm text-red-600">{{ $message }}</div>
@enderror
</div>
<div class="flex gap-4 mb-4">
@foreach($project->images as $image)
<img src="{{ asset('/storage/' . $image) }}" alt="multiple show"
class="w-20 h-20 border border-blue-600">
@endforeach
</div>
<button type="submit"
class="text-white bg-blue-600 rounded text-sm px-5 py-2.5">Update</button>
</form>
</div>
</div>
</div>
</div>
</x-app-layout>
index.blade.php file to show images
edit.blade.php file to to update images.
After update multiple images.
Read Also
Laravel 9 Upload Multiple Image Using Spatie Media Library
How To Upload Multiple Images In Laravel 9 With Intervention
How to update multiple images in laravel 9
Upload Images with Spatie Media Library in Laravel 9