以编辑形式显示现有图像并使用 Laravel 更新它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45907191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:33:34  来源:igfitidea点击:

Display the existing image in edit form and update it using laravel

formslaravel

提问by Abhishek Ganguly

I am able to display the existing text like name and address but unable to display Image in edit form. My show.blade.php

我能够显示名称和地址等现有文本,但无法以编辑形式显示图像。我的 show.blade.php

 @section('content')
        <p>This is user {{ $profile->name }}</p>
        <p>This is address {{ $profile->address }}</p>
         <img src= "/storage/images/{{ $profile->images }}" style="width:504px;height:228px />     
 @endsection 

this works fine.

这工作正常。

now edit.blade.php

现在edit.blade.php

<div class="container">
  <form method="POST" action="/profiles/{{$profile->id}}" enctype="multipart/form-data">
    {{ method_field('PATCH') }}
    {{ csrf_field() }}
    <div class="form-group">
    name  <input type="text" name="name" value="{{ $profile->name }}" />
    </div>
    <div class="form-group">
     address  <input type="textarea" name="address" value="{{ $profile->address }}" />
    </div> 
    <div class="form-group">
    @if ("/storage/images/{{ $profile->images }}")
        <img src="{{ $profile->image }}">
    @else
            <p>No image found</p>
    @endif
        image <input type="file" name="image" value="{{ $profile->images }}"/>
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-primary">Submit</button>
    </div>``

</div>

when I try to save the form a diffrent images path is saved in database table when updating the images ie

当我尝试保存表单时,更新图像时将不同的图像路径保存在数据库表中

 /tmp/phpHs0Io6

store controller is

商店控制器是

public function store(Request $request)
{
       $this->validate($request, [
        'name' => 'required|max:255',
         'address' => 'nullable' ,
    ]);
       $profile = new profile;
       $profile-> user_id = auth()->id();
       $profile-> name = $request->name;
       $profile-> address = $request->address;         
       $request->image->store('public/images');
       $path = $request->image->hashName();
       $profile-> images = $path;
       $profile->save();
       return redirect('/home');
}

and update controller is

和更新控制器是

public function update(Request $request, profile $profile)
    {
        $this->validate($request, [
            'name' => 'required|max:255',
             'address' => 'nullable' ,
        ]);
           $profile->update($request->all());
           $profile-> name = $request->name;
           $profile-> address = $request->address;         
           $request->image->store('public/images');
           $path = $request->image->hashName();
           $profile-> images = $path;
           $profile->save();
           return redirect('/home');
    }

回答by Leo

First of all before you save an image you have to check if user uploaded one; Your way of saving image is wrong use this pattern;

首先,在保存图像之前,您必须检查用户是否上传了图像;您保存图像的方式是错误的,请使用此模式;

    public function update(Request $request, profile $profile)
    {
        $this->validate($request, [
            'name' => 'required|max:255',
             'address' => 'nullable' ,
        ]);
           $profile->update($request->all());
           $profile-> name = $request->name;
           $profile-> address = $request->address;         

           if(Input::hasFile('image')){
           $file = Input::file('image');
           $destinationPath = public_path(). '/uploads/';
           $filename = $file->getClientOriginalName();
           $file->move($destinationPath, $filename);

          //then proceeded to save user
          $profile-> images =           
          $destinationPath.$filename;
          $profile->save();
          return redirect('/home');
          }else{


           $profile->save();
           return redirect('/home');
        }
}

same on update function.

更新功能相同。

回答by Mr.Gandhi

Instead of directly writing path in if condition, use file_existsfunction to check whether your image exist at the given location. For more info check this link

不是直接在 if 条件中写入路径,而是使用file_exists函数来检查您的图像是否存在于给定位置。有关更多信息,请查看此链接