2016-04-16 72 views
0

我有這個表單編輯文章,更改照片:Laravel上傳的照片上更新

<h1>Edit: {{$article->title}}</h1> 
       <hr> 

       {!! Form::model($article, ['method'=>'PATCH','files' => true, 'action'=>['[email protected]', $article->id]]) !!} 
       @include('articles.form',['submitButtonText'=>'Update Article']) 

       {!! Form::close() !!} 


       @include('errors.list') 
現在控制器

我有這樣的功能:

public function update($id, Requests\ArticleRequest $request) 
{ 
    $photo= 'http://nationaluasi.com/dru/content/hotelIcon.png'; 
    $file = array('photo' => $request->file('photo')); 

    // setting up rules 
    $rules = array('photo' => 'required',); //mimes:jpeg,bmp,png and for max size max:10000 

    // doing the validation, passing post data, rules and the messages 
    $validator = Validator::make($file, $rules); 

    if ($validator->fails()) { 
     // send back to the page with the input data and errors 
     $photo = 'http://nationaluasi.com/dru/content/hotelIcon.png';    
    } 
    else { 
    // checking file is valid. 
     if ($request->file('photo')->isValid()) { 
      $destinationPath = public_path().'/images'; // upload path 
      $extension = $request->file('photo')->getClientOriginalExtension();    // getting image extension 
      $photo = str_random(5).'.'.$extension; // renameing image 
      $request->file('photo')->move($destinationPath, $photo); // uploading file to given path 
      // sending back with message 

     } 
     else { 

     } 
    } 

    $article = Auth::user()->articles()->findOrFail($id); 
    $article['photo'] = $photo; 
    $article->update($request->all()); 

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

    return redirect('auctions'); 
} 

但現在當我試圖提交上傳照片我得到這個結果在列photoC:\wamp\tmp\php21F4.tmp,但也形象上傳到/images文件夾...

這裏有什麼問題?如何更新的文章......我也想說,就是一切正常,當我添加的文章 - 添加照片等方法store一切都是相同的,做工精細...

UPDATE:

我嘗試:

$article = Auth::user()->articles()->findOrFail($id); 
     $article['photo'] = $photo; 
     dd($photo); 

,一切都很好,照片上傳成功地只是我不更新的文章[「照片」] ...所以問題就在這裏:

$物品─>更新($請求 - >所有());

但是如何解決呢?爲什麼文章['照片']沒有更新?

+0

如果您嘗試$ article-> photo = $ photo; $物品─)>保存(; –

+0

不,再次是一樣的... – Andrew

回答

1

我試圖糾正你的代碼,使用它。

public function update($id, Requests\ArticleRequest $request) 
    { 
     $this->validate($request, [ 
      'photo' => 'required|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'); 
    } 
+1

是的,這項工作,非常感謝! – Andrew

1

我假設你正在使用Laravel 5.我相信這個問題是這一行:

$article->update($request->all()); 

更新方法需要列和值對的數組。您的上面的代碼是給它的原始(未修改)請求,其中不包括您的新照片位置。

使用Laravel,您實際上可以獲取文章對象,將該列直接設置在對象內,然後保存更改。

試着改變你的方法的最後幾行:

$article = Auth::user()->articles()->findOrFail($id); // get the article 
    $article->photo = $photo; // set the photo column to your new location 
    $article->save(); // this will save your changes 

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

    return redirect('auctions'); 

看看在Laravel documentation更多的解釋。

+0

但我也有許多其他數據,如$ article-> name ...如何包含所有這些數據? – Andrew

+0

在我看來,你應該以類似的方式進行更新,因爲你應該在保存之前驗證用戶輸入。 –

+0

但與上面的答案有驗證... – Andrew