Storm Spirit
Storm Spirit

Reputation: 1520

Laravel - Cannot set and get the filename of uploaded file

I've got problem setting the avatar name so I can put to the database, it shows an object and not the filename, as you can see I've echo $filename to make sure I've got the name of the image. but when I print_r($sanitized), the image is an object.

enter image description here

My expected result of $sanitized should be:

Array
(
    [email] => [email protected]
    [name] => Superadmin
    [phone] => 123123
    [avatar] => 1_avatar1546579727.jpg
)

Code:

public function updateProfile(Request $request)
{
    $this->setUser($request);
    $user = $this->user;

    // Validate the request
    $this->validate($request, [
        'email' => ['sometimes', 'email', Rule::unique('users', 'email')->ignore($this->user->getKey(), $this->user->getKeyName()), 'string'],
        'name' => ['nullable', 'string'],
        'phone' => ['sometimes', 'string'],
        'avatar' => ['sometimes', 'image', 'mimes:jpeg,png,jpg,gif', 'dimensions:min_width=500,min_height=500', 'max:2048'],
    ], [
        'avatar.mimes'      => 'Uploaded file format should be jpeg, jpg, png or gif.',
        'avatar.dimensions' => 'Image should have minimum 200x200px dimensions.',
        'avatar.max'        => 'Maximum allowed file size is 2 MB.',
    ]);

    if($request->hasFile('avatar')) {
        $filename = $user->id.'_avatar'.time().'.'.request()->avatar->getClientOriginalExtension();
        Image::make(request()->avatar)->resize(300, 300)->save( public_path('uploads/avatars/'.$filename) );

        // $request->avatar = $filename;
        $request['avatar'] = $filename;
    }

    // Sanitize input
    $sanitized = $request->only([
        'email',
        'name',
        'phone',
        'avatar'
    ]);

    echo $filename . "</br>";

    echo "<pre>";
    print_r( $sanitized );
    echo "</pre>";

    return "";
        // $this->user->update($sanitized);
    // return redirect()->back()->with('success', 'Profile has been updated.');
}

EDIT 1

I've tried all your answers, it still the same results. Code:

if($request->hasFile('avatar')) {
    $filename = $user->id.'_avatar'.time().'.'.request()->avatar->getClientOriginalExtension();
    Image::make(request()->avatar)->resize(300, 300)->save( public_path('uploads/avatars/'.$filename) );

    //This is what I've tried so far below: 
    $request->request->add(['avatar', $filename]); 
    // $request->merge(['avatar' => $filename]);
    // $request->avatar = $filename;
    // $request['avatar'] = $filename;
}

Upvotes: 0

Views: 872

Answers (1)

Storm Spirit
Storm Spirit

Reputation: 1520

I've just fixed it by just giving me a hint of @Md.Sukel Ali

I've moved the $sanitzed = $request.. to the top, then update it and not using directly $request variable.

Working Code:

public function updateProfile(Request $request)
{
    $this->setUser($request);
    $user = $this->user;

    // Validate the request
    $this->validate($request, [
        'email' => ['sometimes', 'email', Rule::unique('users', 'email')->ignore($this->user->getKey(), $this->user->getKeyName()), 'string'],
        'name' => ['nullable', 'string'],
        'phone' => ['sometimes', 'string'],
        'avatar' => ['sometimes', 'image', 'mimes:jpeg,png,jpg,gif', 'dimensions:min_width=500,min_height=500', 'max:2048'],
    ], [
        'avatar.mimes'      => 'Uploaded file format should be jpeg, jpg, png or gif.',
        'avatar.dimensions' => 'Image should have minimum 200x200px dimensions.',
        'avatar.max'        => 'Maximum allowed file size is 2 MB.',
    ]);

    // Sanitize input
    $sanitized = $request->only([
        'email',
        'name',
        'phone',
        'avatar'
    ]);

    if($request->hasFile('avatar')) {
        $filename = $user->id.'_avatar'.time().'.'.request()->avatar->getClientOriginalExtension();
        Image::make(request()->avatar)->resize(300, 300)->save( public_path('uploads/avatars/'.$filename) );
        $sanitized['avatar'] = $filename;
    }

    echo $filename . "</br>";

    echo "<pre>";
    print_r( $sanitized );
    echo "</pre>";

    return "";

    // $this->user->update($sanitized);
    // return redirect()->back()->with('success', 'Profile has been updated.');
}

Upvotes: 1

Related Questions