'Laravel - Update function not keeping current image if new image is not uploaded
I want to keep the current image if new image is not uploaded on the update function, but right now it is not working. The texts are working. I f I dont input new text, the old text is kept, but I haven´t been able to keep the old the image files if a new one is not uploaded. Anny suggestions on how to make this work? Thanks in advance.
Update function and functions to update images in the controller:
public function update(Request $request, LandingPage $landing)
{
$input = $request->all();
$input['main_gif'] = $this->updateMainGif($request, uniqid());
$input['product1'] = $this->updateProduct1($request, uniqid());
$input['product2'] = $this->updateProduct2($request, uniqid());
$input['product3'] = $this->updateProduct3($request, uniqid());
$input['product4'] = $this->updateProduct4($request, uniqid());
$landing->update($input);
return redirect()->route('landings.index')->with('message', "Landing successfully added");
}
private function updateMainGif($request, $name){
$input = $request->all();
if ($request->hasFile('main_gif')) {
if ($request->file('main_gif')->isValid()) {
$file = $request->file('main_gif');
$extension = $file->extension();
$file->storeAs('public', $name.".".$extension);
return asset("storage/".$name.".".$extension);
}
unset($input['main_gif']);
}
}
private function updateProduct1($request, $name){
$input = $request->all();
if ($request->hasFile('product1')) {
if ($request->file('product1')->isValid()) {
$file = $request->file('product1');
$extension = $file->extension();
$file->storeAs('public', $name.".".$extension);
return asset("storage/".$name.".".$extension);
}
unset($input['product1']);
}
}
private function updateProduct2($request, $name){
$input = $request->all();
if ($request->hasFile('product2')) {
if ($request->file('product2')->isValid()) {
$file = $request->file('product2');
$extension = $file->extension();
$file->storeAs('public', $name.".".$extension);
return asset("storage/".$name.".".$extension);
}
unset($input['product2']);
}
}
private function updateProduct3($request, $name){
$input = $request->all();
if ($request->hasFile('product3')) {
if ($request->file('product3')->isValid()) {
$file = $request->file('product3');
$extension = $file->extension();
$file->storeAs('public', $name.".".$extension);
return asset("storage/".$name.".".$extension);
}
unset($input['product3']);
}
}
private function updateProduct4($request, $name){
$input = $request->all();
if ($request->hasFile('product4')) {
if ($request->file('product4')->isValid()) {
$file = $request->file('product4');
$extension = $file->extension();
$file->storeAs('public', $name.".".$extension);
return asset("storage/".$name.".".$extension);
}
unset($input['product4']);
}
}
edit.blade.file
@extends('layouts.app')
@section('content')
<div class="container">
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
<div class="card-body">
<form method="POST" action="{{ route('landings.update', $landing->id) }}" enctype="multipart/form-data">
@csrf @method("PUT")
<div class="form-group row">
<label for="url" class="col-md-4 col-form-label text-md-right">{{ __('URL') }}</label>
<div class="col-md-6">
<input id="url" type="text" class="form-control @error('url') is-invalid @enderror" name="url" value="{{ $landing->url }}" required autocomplete="url" autofocus>
@error('url')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="main_gif" class="col-md-4 col-form-label text-md-right">{{ __('Product 1') }}</label>
<div class="col-md-6">
@if($landing->main_gif)
<img class="img-fluid" src="{{ $landing->main_gif }}">
@endif
<input id="main_gif" type="file" class="form-control @error('main_gif') is-invalid @enderror" name="main_gif">
@error('main_gif')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="product1" class="col-md-4 col-form-label text-md-right">{{ __('Product 1') }}</label>
<div class="col-md-6">
@if($landing->product1)
<img class="img-fluid" src="{{ $landing->product1 }}">
@endif
<input id="product1" type="file" class="form-control @error('product1') is-invalid @enderror" name="product1">
@error('product1')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="text1" class="col-md-4 col-form-label text-md-right">{{ __('Product Title 1') }}</label>
<div class="col-md-6">
<input id="text1" type="text" class="form-control @error('text1') is-invalid @enderror" name="text1" value="{{ $landing->text1 }}" required autocomplete="text1" autofocus>
@error('text1')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="product_link1" class="col-md-4 col-form-label text-md-right">{{ __('Product Link 1') }}</label>
<div class="col-md-6">
<input id="product_link1" type="text" class="form-control @error('product_link1') is-invalid @enderror" name="product_link1" value="{{ $landing->product_link1 }}" required autocomplete="product_link1" autofocus>
@error('product_link1')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="product2" class="col-md-4 col-form-label text-md-right">{{ __('Product 2') }}</label>
<div class="col-md-6">
@if($landing->product2)
<img class="img-fluid" src="{{ $landing->product2 }}">
@endif
<input id="product2" type="file" class="form-control @error('product2') is-invalid @enderror" name="product2">
@error('product2')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="text2" class="col-md-4 col-form-label text-md-right">{{ __('Product Title 2') }}</label>
<div class="col-md-6">
<input id="text2" type="text" class="form-control @error('text2') is-invalid @enderror" name="text2" value="{{ $landing->text2 }}" required autocomplete="text2" autofocus>
@error('text2')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="product_link2" class="col-md-4 col-form-label text-md-right">{{ __('Product Link 2') }}</label>
<div class="col-md-6">
<input id="product_link2" type="text" class="form-control @error('product_link2') is-invalid @enderror" name="product_link2" value="{{ $landing->product_link2 }}" required autocomplete="product_link2" autofocus>
@error('product_link2')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="product3" class="col-md-4 col-form-label text-md-right">{{ __('Product 3') }}</label>
<div class="col-md-6">
@if($landing->product3)
<img class="img-fluid" src="{{ $landing->product3 }}">
@endif
<input id="product3" type="file" class="form-control @error('product3') is-invalid @enderror" name="product3">
@error('product3')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="text3" class="col-md-4 col-form-label text-md-right">{{ __('Product Title 2') }}</label>
<div class="col-md-6">
<input id="text3" type="text" class="form-control @error('text3') is-invalid @enderror" name="text3" value="{{ $landing->text3 }}" required autocomplete="text3" autofocus>
@error('text3')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="product_link3" class="col-md-4 col-form-label text-md-right">{{ __('Product Link 3') }}</label>
<div class="col-md-6">
<input id="product_link3" type="text" class="form-control @error('product_link3') is-invalid @enderror" name="product_link3" value="{{ $landing->product_link3 }}" required autocomplete="product_link3" autofocus>
@error('product_link3')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="product4" class="col-md-4 col-form-label text-md-right">{{ __('Product 4') }}</label>
<div class="col-md-6">
@if($landing->product4)
<img class="img-fluid" src="{{ $landing->product4 }}">
@endif
<input id="product4" type="file" class="form-control @error('product4') is-invalid @enderror" name="product4">
@error('product4')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="text4" class="col-md-4 col-form-label text-md-right">{{ __('Product Title 4') }}</label>
<div class="col-md-6">
<input id="text4" type="text" class="form-control @error('text4') is-invalid @enderror" name="text4" value="{{ $landing->text4 }}" required autocomplete="text4" autofocus>
@error('text4')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="product_link4" class="col-md-4 col-form-label text-md-right">{{ __('Product Link 4') }}</label>
<div class="col-md-6">
<input id="product_link4" type="text" class="form-control @error('product_link4') is-invalid @enderror" name="product_link4" value="{{ $landing->product_link4 }}" required autocomplete="product_link4" autofocus>
@error('product_link4')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Adicionar') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
