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.