Reputation: 1639
I have this function where I want to update user background image so I write:
public function updateBg(Request $request)
{
$user = Auth::user();
$this->validate($request, [
'background' => 'image|max:10000',
// validate also other fields here
]);
// checking file is valid.
if (!$request->file('background')->isValid()) return redirect()->back()->withErrors(["background" => "File is corrupt"]);
// file is valid
$destinationPath = public_path().'/images/Custaccounts/'.$user->id; // upload path
$extension = $request->file('background')->getClientOriginalExtension(); // getting image extension
$ran = str_random(5);
$photo = $ran.'.'.$extension;
$request->file('background')->move($destinationPath, $photo); // uploading file to given path
$bg = $user->id.'/'.$photo;
dd($bg);
$user->update(['background' => $bg]);
return Redirect::back();
}
this line wont work: $user->update(['background' => $bg]);
dd($bg) give me right string and image is uploaded just I cant update my 'background; field ...
also when I write:
$user->update(['background' => $bg, 'name'=>'John']);
name is updated but background not... at user model background field is fillable of cource
Upvotes: 0
Views: 91
Reputation: 4248
If i understand your problem you are trying to update the bg field form User table.. Your code must work. If not working Try this:-
Remove this :- $user->update(['background' => $bg]);
Update this :- User::where('id',Auth::user()->id)->update(['background' => $bg]);
Hope it helps!
Upvotes: 1