2017-08-27 53 views
0

我能夠顯示現有文本,如名稱和地址,但無法在編輯窗體中顯示圖像。 我的show.blade.php在編輯表單中顯示現有圖像並使用laravel進行更新

@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 

這工作正常。

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

回答

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

與更新功能相同。

0

不是在if條件下直接寫入路徑,而是使用file_exists函數來檢查您的圖像是否存在於給定位置。欲瞭解更多信息,請點擊這裏link

相關問題