Laravel 更新时上传照片

Laravel upload photo on update

我有这个表格来编辑文章和更改照片:

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

                {!! Form::model($article, ['method'=>'PATCH','files' => true, 'action'=>['ArticlesController@update', $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');
}

但是现在当我尝试提交上传的照片时,我在 photo 列中得到了这个结果:C:\wamp\tmp\php21F4.tmp 但图像也被上传到 /images 文件夹中...

这里有什么问题?如何更新文章...我还想说当我添加文章时一切都很好 - 添加照片等方法 store 一切都一样并且工作正常...

更新:

我试试:

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

一切正常,照片上传成功只是我没有更新文章['photo']...所以问题就在这里:

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

但是怎么解决呢?为什么文章['photo']没有更新?

我试图更正您的代码,请使用它。

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

我假设您正在使用 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 以获得更多解释。