2016-04-18 5 views
0

私はここにフィールドを更新し、この機能を持つので、私のコードがあるPHOTO

imporant入力で、Laravel更新機能を持っているnullの場合:私は写真のすべてを更新するために、いくつかの画像を選択すると

public function update($id, Requests\ArticleRequest $request) 
    { 
     $this->validate($request, [ 
      'photo' => 'image|max:10000', 
      // validate also other fields here 
     ]); 
     // checking file is valid. 
     if (!$request->file('photo')->isValid()) return redirect()->back()->withErrors(["photo" => "File is corrupt"]); 

     // file is valid 
     $destinationPath = public_path().'/images'; // upload path 
     $extension = $request->file('photo')->getClientOriginalExtension(); // getting image extension 
     $filename = str_random(5).'.'.$extension; // give a name to the image 
     $request->file('photo')->move($destinationPath, $filename); // uploading file to given path 
     // sending back with message 

     $article = Auth::user()->articles()->findOrFail($id); //if article id is unique just write Article::findOrFail($id) 
     $article_fields = $request->except('photo'); 
     $article_fields['photo'] = $filename; 
     $article->update($article_fields); 

     Alert::message('Your auction is updated', 'Wonderful!'); 

     return redirect('auctions'); 
      } 

はとても細かいですが、私はデータベースで同じ滞在する他のfiledsや写真を更新したいとき...私はエラーを取得する:

Call to a member function isValid() on a non-object 

$要求 - >ファイルが(「写真」)ので、新たな画像が選びだしていない、空白である場合、私は...

+0

'もし(!$要求 - >ファイル(「写真」)写真をスキップすることができますどのように'です十分な –

+0

しかし、問題はコードの最後にあります:$ article_fields ['photo'] = $ filename;これは私の写真の列を更新します...これをスキップする方法は? – Andrew

+0

ファイルがない場合は、その列の更新をスキップするだけです。 –

答えて

0
public function update($id, Requests\ArticleRequest $request){ 
     **//check if file provided** 
     if ($request->hasFile('photo')) { 

      $this->validate($request, [ 
      'photo' => 'image|max:10000', 
      // validate also other fields here 
     ]); 
     // checking file is valid. 
     if (!$request->file('photo')->isValid()) return redirect()->back()->withErrors(["photo" => "File is corrupt"]); 

     // file is valid 
     $destinationPath = public_path().'/images'; // upload path 
      $extension = $request->file('photo')->getClientOriginalExtension(); // getting image extension 
      $filename = str_random(5).'.'.$extension; // give a name to the image 
      $request->file('photo')->move($destinationPath, $filename); // uploading file to given path 
     // sending back with message 

      $article = Auth::user()->articles()->findOrFail($id); //if article id is unique just write Article::findOrFail($id) 
      $article_fields = $request->except('photo'); 
      $article_fields['photo'] = $filename; 
      $article->update($article_fields); 

      Alert::message('Your auction is updated', 'Wonderful!'); 

      return redirect('auctions'); 
     } 
    Alert::message('Nothing to update', 'So sad!'); 
return redirect('auctions'); 
} 
関連する問題