tgif's avatar
Level 4

implementing avatar in User model

Hi Guys, I'm implementing an avatar feature using the User model with an extra column added: 'avatar'.

I'm following along the Laracast Bulk File Uploads and I have everything working except the code which saves the path of the image to the User->avatar field.

I need a little help persisting the string. This is what I have so far...

in the User model

public function saveAvatar($name)
    {
        $this->fill(['avatar' => $name]);
    }

in the Auth controller...

protected function postAvatar($id, Request $request)
{
        $file = $request->file('file');
        $name = time() . $file->getClientOriginalName();
        $user = User::where('id', $id)->get();
    $user->avatar = $name;
    $user->save();
}

But I get this error:

FatalErrorException in AuthController.php line 100: Call to undefined method Illuminate\Database\Eloquent\Collection::save()

0 likes
2 replies
bobbybouwmann's avatar
Level 88

You can't run the save method on the collection, like the error says. This should work for you

protected function postAvatar($id, Request $request)
{
    $file = $request->file('file');
    $name = time() . $file->getClientOriginalName();

    $user = User::findOrFail($id);
    $user->avatar = $name;
    $user->save();
}

Please or to participate in this conversation.