How to display the name of the image that is in the bank in the view with Laravel?

Asked

Viewed 239 times

0

Good night.

I’m having problems to update an image with Laravel in Mysql database.

My controller’s store method is 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, is this way:

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>

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 keep it if no changes are made to the image, having changes only in the texts.

  • I don’t quite understand your problem, when you update if an image already exists you want to kill it when a new image is not sent? that’s your problem ?

  • That’s exactly it. How would I do that?

1 answer

0


You need to put a condition to check if any files have been uploaded. To do this just use the method hasFile().

In your case it would look something like this:

public function update(Request $request, $id) {
    $banners = Banner::findOrFail($id);
    $banners->titulo = $request->titulo;
    $banners->descricao = $request->descricao;
    if ($request->hasFile('imagem')) {    
        $banners->imagem = $request->file('imagem')->storeAs('banners');
    }
    $banners->save();
    return redirect()->route('banner.index')->with('message', 'Banner alterado com sucesso!');
}

Even in the Lades you only have to create a condition:

@if($banner->imagem) 
<!-- Aqui dentro HTML, recomendo colocar uma img acerte o Css (adeque o src ao caminho publico da sua imagem) -->    
<img src="{{$banner->imagem}}" />
@endif

Browser other questions tagged

You are not signed in. Login or sign up in order to post.