php - Laravel 5.4 - Updating a File record -
i'm pretty new laravel, i'm trying learn of basics. have managed create crud file upload. did created resource controller. 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 added!'); }
this method works fine, able upload , store details database , able manipulate in view.
but when try use update method have exact code.. yet when try update post can updated except avatar variable. updates stores in strange format. here 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 updated!'); return redirect('/management'); }
when edit avatar variable stores in /img/boats/avatars/c:\xampp\tmp\phpfa50.tmp
i'm not sure wrong. appreciated.
$boat->save() ;
don't need save before update -> remove it$boat_update = $request->all();
-> filename not in there$boat->update($boat_update);
fields$fillable
? if yes need add new filename array
Comments
Post a Comment