以编辑形式显示现有图像并使用laravel进行更新

时间:2017-08-27 16:48:29

标签: forms laravel

我能够显示现有文本,例如姓名和地址,但无法以编辑形式显示图像。 我的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 

这很好。

now 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>

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

 /tmp/phpHs0Io6

商店控制器

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');
}

并且更新控制器是

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');
    }

3 个答案:

答案 0 :(得分:0)

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

    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');
        }
}

更新功能相同。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我有点晚了,但我有一个建议: 因此,如果您的图像正在上传到存储中,此代码可能会起作用,您仍然需要调整图像大小,使其不会覆盖整个站点:

                                <div class="pull-right">
                                    <label for="image">Choosen Datei:</label>
                                    <img src="{{Storage::disk('local')->url($profile->image)}}" alt="">
                                    <label for="image"
                                        >Choose data</label
                                    >
                                    <input
                                        type="file"
                                        name="image"
                                        id="image"
                                        class="form-control"
                                    />
                                </div>