Andrew Andrew - 7 months ago 44
PHP Question

Laravel upload photo on update

I have this form to edit article and change photo:

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


now at Controller I have this function:

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


but now when I try to submit uploaded photo I get this result in column
photo
:
C:\wamp\tmp\php21F4.tmp
but also image is uploaded into
/images
folder...

What is the problem here? How to update article... also I want to say that is everything fine when I add article - add photo so on method
store
everything is the same and work fine...

UPDATE:

I try:

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


and everything is fine, photo is uploaded succesfully just i dont update article['photo']... so problem is here:

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

But how to solve it? Why article['photo'] is not updated ?

Answer

I tried to correct your code, use this.

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