简体   繁体   中英

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 ?

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

I'll assume you're using Laravel 5. I believe the issue is this line:

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

The update method expects an array of column and value pairs. Your code above is giving it the original (unmodified) request which doesn't include your new photo location.

With Laravel you can actually get the article object, set the column directly within the object and then save the changes.

Try changing the last few lines of your method to:

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

Take a look at the Laravel documentation for more explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM