2017-08-31 44 views
0

我是Laravel的新手,所以我真的需要一些幫助。我想問什麼時候 我評論部分'photo'=>要求爲什麼如果我更新而不輸入照片它顯示一些錯誤,如調用成員函數getClientOriginalName()null。所以真正的問題是我想更新而不輸入照片,它應該仍然是更新。PHP - 調用成員函數getClientOriginalName()null並刪除警報laravel

這是我在控制器代碼上傳照片

public function update($id, UpdateBannerRequest $request) 
{ 
    $input = $request->all(); 
    //get original file name 
    $filename = Input::file('photo')->getClientOriginalName(); 
    $input['photo'] = $filename; 
    Input::file('photo')->move($this->path, $filename); 
    $banner = $this->BannerRepository->findWithoutFail($id); 

    if (empty($banner)) { 
     Flash::error('Banner not found'); 

     return redirect(route('banner.index')); 
    } 

    $banner = $this->BannerRepository->update($input, $id); 

    Flash::success('Banner updated successfully.'); 

    return redirect(route('banner.index')); 
} 

這是我的模型

<?php 

命名空間應用\模型的代碼;

使用Eloquent as Model; 使用Illuminate \ Database \ Eloquent \ SoftDeletes;

class Banner extends Model { use SoftDeletes;

public $table = 'banners'; 


protected $dates = ['deleted_at']; 


public $fillable = [ 
    'title', 
    'description', 
    'photo',   
    'status' 
]; 

protected $casts = [ 
    'title' => 'string', 
    'description' => 'string', 
    'photo' => 'string',  
    'status' => 'integer' 
]; 



public static $rules = [ 
    'title' => 'required', 
    'description' => 'required', 
    //'photo' => 'required', 
    'status' => 'required' 
]; 

}

This is the view and This is the error

+0

'Input :: file('photo')'不是一個對象 – Scuzzy

+0

所以當沒有進入照片的更新會出現'沒有找到圖片'像這樣的照片http://imgur.com/a/qzDVc –

+0

@Scuzzy hello感謝您的回覆,那麼我該如何處理這個問題? –

回答

1
$validator = Validator::make(
        $request->all(), 
        array(
         'photo' => 'required', 
        ), 
        array(
         'photo' => 'Please choose file', 
        ) 
       ); 

如果照片是不是強制性的直接使用此

if(!empty($request->photo)){ 
    //do something 
} 
else{ 
    Flash::error('Banner not provided'); 
    return redirect(route('banner.index')); 
} 

希望這將有助於..讓我知道,如果任何issue..Thank你

您的更新功能將類似於

 public function update($id, UpdateBannerRequest $request) 
     { 
      $input = $request->all(); 
      $banner = $this->BannerRepository->findWithoutFail($id); 
      if(!empty($request->photo)){ 
       //do something for saving the name of file in database and other value respectively using 
    //   $filename = Input::file('photo')->getClientOriginalName(); 
    //   $banner->photo = $filename; 
      } 
      else{ 
       Flash::error('Banner not provided'); 
       return redirect(route('banner.index')); 
      } 
      $banner->save(); 
      Flash::success('Banner updated successfully.'); 
      return redirect(route('banner.index')); 
     } 
+0

感謝您的幫助:DD是$驗證放在模型Banner.php? –

+0

標記它接受,如果它是有用的,所以其他人可以得到benfits –

+0

後來我將它標記爲接受,但我問的東西是放在我的模型$驗證器? –

0

你叫Input::file('photo')->getClientOriginalName()

if(Input::hasFile('photo') == false) 
{ 
    Flash::error('Banner not provided'); 
    return redirect(route('banner.index')); 
} 

https://laravel.com/docs/4.2/requests#files

之前,將需要最簡單的驗證測試是否 Input::hasFile('photo'),這應該放在
+0

感謝您的幫助:D但它仍然沒有工作這是代碼http://imgur.com/a/9wqCM,這是錯誤http://imgur.com/a/2znco –

+0

我試着在Input :: file('photo') - > getClientOriginalName()之前放置並仍然顯示同樣的錯誤http:// imgur。com/a/ea62h –

+0

oops,我把一個括號更改爲'if(Input :: hasFile('photo')== false)' – Scuzzy

0

您應該檢查波紋管代碼。

if(isset(Input::file('photo'))

與它的工作之前。

+0

感謝您的幫助,但如何使它在我的代碼,因爲我真的新手在Laravel:D –

+0

$ file = $ request-> file('photo'); 如果(isset($文件)){// 這裏做上傳代碼 }其他{// 如果文件不存在 } –

+0

感謝您的幫助。但我沒有得到它,你可以使完整的代碼,請我真的需要一些幫助:DDD –

相關問題