0
votes

I was trying to update my userprofile with the following controller but the problem is if i update only profile picture it shows the above error..But if i update every value it update successfully. How do i update the userProfile without updating every value :

public function updateUser(Request $request)
{
    $this->validate($request, [
        'profile_picture' => 'dimensions:width=400,height=400',
        'cover_picture' => 'dimensions:width=800,height=400',
        'avatar' => 'dimensions:width=80,height=80',            
        ]);

if (\Auth::check())
{
    $user= User::find(\Auth::id());
}

$files= [];
if($request->file('profile_picture'))   $files[] = $request->file('profile_picture');
if($request->file('cover_picture'))   $files[] = $request->file('cover_picture');
if($request->file('avatar'))   $files[] = $request->file('avatar');

foreach($files as $file)
    {
        if(!empty($file))
        {
            $filename = time().str_random(20). '.' . $file->getClientOriginalExtension();
            $file->move('users/',$filename);      
            $filenames[]=$filename;
        }
    }

$user->profile_picture = $filenames[0];
$user->cover_picture = $filenames[1];
$user->avatar = $filenames[2];

$user->save();
return redirect::back()->with('Warning',"Profile Updated Successfully");

}
2
Include a var_dump of $filenames please. My assumption is $filenames[1] doesn't exist. - Scuzzy
$filenames[1] doesn't exist .. because i just uploaded profile_picture not the cover_picture ..suppose i don't want to upload that. then i got the following error. - User57
Yeah, See my answer, I belive I've re-strucutred your code into a more concise approach. - Scuzzy

2 Answers

1
votes

I don't think it's wise using a positional array like this, As you've discovered, what if someone only wants to update their avatar. I feel your assignment into $files[] is redundant and you could go straight into your processing code.

Basically your current implementation means $files can be of a variable length, how do you know which is 0, 1 or 2 etc ?

With my approach, the code is now looping over each type of picture, and assigns it into the user with $user->$type directly by the same matching type property.

foreach( array( 'profile_picture', 'cover_picture', 'avatar' ) as $type)
{
    if( $request->file( $type ) )
    {
        $filename = time() . str_random(20) . '.' . $request->file( $type )->getClientOriginalExtension();
        $request->file( $type )->move( 'users/', $filename );      
        $user->$type = $filename;
    }
}

If you find you need to map a different $source to the $type variable, you could do this with an additional array index...

foreach( array(
  'profile_picture' => 'profile_picture',
  'cover_picture' => 'cover_picture',
  'avatar' => 'avatar'
  ) as $source => $type)
{
    if( $request->file( $source ) )
    {
        $filename = time() . str_random(20) . '.' . $request->file( $source )->getClientOriginalExtension();
        $request->file( $source )->move( 'users/', $filename );      
        $user->$type = $filename;
    }
}
-3
votes

I finally came up with a solution mate.

You can try to Include a var_dump of $filenames. I suppose that $filenames[1] doesn't exist at all.