Good evening.
I'm having trouble performing an update on an image with Laravel on the Mysql database.
The store method of my controller looks like this:
public function store(Request $request){
$filename = $request->imagem->getClientOriginalName();
$banners = new Banner;
$banners->titulo = $request->titulo;
$banners->descricao = $request->descricao;
$banners->imagem = $request->file('imagem')->storeAs('banners/', $filename);
$banners->save();
return redirect()->route('banner.index')->with('message', 'Banner publicado com sucesso!');
}
And update method, it looks like this:
public function update(Request $request, $id) {
$banners = Banner::findOrFail($id);
$banners->titulo = $request->titulo;
$banners->descricao = $request->descricao;
$banners->imagem = $request->file('imagem')->storeAs('banners');
$banners->save();
return redirect()->route('banner.index')->with('message', 'Banner alterado com sucesso!');
}
My view Change this way:
<div class="row">
<div class="col-12">
<form method="post" action="{{ route('banner.update', $banner->id) }}" enctype="multipart/form-data">
{{ method_field('PATCH') }}
{{ csrf_field() }}
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-lg-12">
<div class="form-row">
<div class="form-group col-lg-9">
<label for="simpleinput">Título</label>
<input type="text" id="simpleinput" value="{{ $banner->titulo }}" class="form-control" name="titulo">
</div>
</div>
<div class="form-row">
<div class="form-group col-lg-9">
<label for="example-fileinput">Imagem</label><i class="mdi mdi-information" data-toggle="tooltip" data-placement="top" title="" data-original-title="Tamanho recomendado 1920pxx800px"></i>
<input type="file" id="example-fileinput" value="{{ $banner->imagem }}" class="form-control-file" name="imagem" accept=".gif,.jpg,.png">
</div>
</div>
<div class="form-row">
<div class="form-group col-lg-9">
<label for="simpleinput">Descrição</label>
<textarea rows="4" id="example-textarea" class="form-control" name="descricao">{{ $banner->descricao }}</textarea>
</div>
</div>
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Publicar</button>
<a type="button" class="btn btn-link" href="{{route('banner.index')}}">Voltar</a>
</form>
In this way, the only thing you can do is replace one image with another.
I would like to know how I can display the name of the image that is in the database and maintain it, if no changes are made to the image, with changes only in the texts.