kendrick's avatar

Avatar Upload / Error: Image will be stored, but not referenced within DB {Creating default object from empty value}?

public function index(){

    return view ('profile.editavatar');
}

public function store(Request $request){

    $this->validate($request, [
        'avatar' => 'max:255',

    ]);

    Auth::user()->update([
        'avatar' => $request->input('avatar'),
    ]);

    if($request->hasFile('avatar')){

        $user = Auth::user();

        $image   = $request->file('avatar');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        // $path     = storage_path('/uploads/avatars/'. $user->username . $filename );
        // Storage::makeDirectory($path, 0777, true);
        $location = public_path('uploads/avatars/'. $filename);
        Image::make($image)->resize(300,300)->save($location);
        
        
        $user->avatar = $filename;  
    }


    
}

There is some struggle with the $user variable, as I am editing the profile content with 2 Controllers. On for written content within the profile & one for Avatar Upload. This is the UploadController.

Any suggestions?

0 likes
2 replies
marthz's avatar
marthz
Best Answer
Level 17
    Auth::user()->update([
        'avatar' => $request->input('avatar'),
    ]);

You don't need that, however you need to call $user->save() at the end of your if statement

1 like
kendrick's avatar

Wow, you just solved my problem which I faced for nearly a month. Thank you

Please or to participate in this conversation.