fares.shawa's avatar

Laravel 5.4 - Updating a File record

I'm pretty new to Laravel, thus I'm trying to learn some of the basics. I have managed to create a CRUD with file upload. What I did was I created a resource controller. My store method:

public function store(Request $request) {

    $this->validate($request, [
        'name' => 'required',
        'avatar' => 'required|image|mimes:jpeg,png,jpg,gif,svg',
        'boat_type' => 'required',
        'rooms' => 'required',
        'price_per_hour' => 'required',
        'price' => 'required',
    ]);
    $boat = new Boat($request->input()) ;
        if($file = $request->hasFile('avatar')) {
            $file = $request->file('avatar') ;
            $fileName = $file->getClientOriginalName() ;
            $destinationPath = public_path().'/img/boats/avatars/' ;
            $file->move($destinationPath,$fileName);
            $boat->avatar = $fileName ;
        }
        $boat->save() ;
        return redirect()->route('management.index')
                    ->with('success','New Boat Successfully Added!');
}

This method works perfectly fine, with this I am able to upload and store the details to my database and am able to manipulate it in my view.

But when I try to use the UPDATE method I have the exact code.. yet when I try to update my post everything can be updated except my avatar variable. it updates it but it stores it in a strange format. Here is my UPDATE method

public function update(Request $request, $id) { $this->validate($request, [ 'name' => 'required', 'boat_type' => 'required', 'rooms' => 'required', 'price_per_hour' => 'required', 'price' => 'required', ]);

    $boat = Boat::find($id);
  if($file = $request->hasFile('avatar')) {
            $file = $request->file('avatar') ;
            $fileName = $file->getClientOriginalName() ;
            $destinationPath = public_path().'/img/boats/avatars/' ;
            $file->move($destinationPath,$fileName);
            $boat->avatar = $fileName ;
        }
        $boat->save() ;
    $boat_update = $request->all();
    $boat->update($boat_update);
    $request->session()->flash('alert-success', 'Boat Successfully Updated!');
    return redirect('/management');
}

When I edit the avatar variable it stores it in /img/boats/avatars/C:\xampp\tmp\phpFA50.tmp

I'm not really sure what is wrong. Any help will be greatly appreciated.

0 likes
3 replies
thomaskim's avatar
Level 41

If I were to guess, it would be this part right here:

        $boat->avatar = $fileName ;
    }
    $boat->save() ;
    $boat_update = $request->all();
    $boat->update($boat_update);

You are setting the file name and saving it. But right after you do that, you are overwriting it with $request->all() and updating it. I would try this instead:

$boat = Boat::find($id);

// This is like $request->all except it doesn't set the avatar
// You "fill" the new data so that it doesn't call save yet.
$boat->fill($request->except('avatar'));

// If there is a file, we set the avatar
if($file = $request->hasFile('avatar')) {
    $file = $request->file('avatar') ;
    $fileName = $file->getClientOriginalName() ;
    $destinationPath = public_path().'/img/boats/avatars/' ;
    $file->move($destinationPath,$fileName);
    $boat->avatar = $fileName ;
}

// Then we just save
$boat->save();

$request->session()->flash('alert-success', 'Boat Successfully Updated!');
return redirect('/management');
1 like
fares.shawa's avatar

Thank you a lot sir, this code was exactly what I wanted it worked!

1 like
shoulieheinds's avatar

I have follow this step, but my problem its not save anything. Just empty....

Please or to participate in this conversation.