1

I am developing an api endpoint to use with my laravel and vue app.

public function avatar(Request $request)
    {
        $user = User::find(Auth::id());
        $validator = Validator::make($request->all(), [
            'avatar' => 'required'
        ]);
        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()]);
        } else {
            $image = $request->get('avatar');
            //base64_decode($file_data)
            $path = Storage::putFile('avatars', base64_decode($image));

            $user->avatar_url = $path;
            if ($user->save()) {
                //return redirect()->route('user_profile_settings');
            }
        }
    }

This is the code that I have I tried going off what I found online to accomplish file uploads with an api and using php, but I am getting this error "Call to a member function hashName() on string". The goal of this is to upload the file to a s3 bucket using the putFile method.

John Freedom
  • 471
  • 4
  • 6
  • 11

1 Answers1

0

I believe your problem lies here:

$image = $request->get('avatar');
$path = Storage::putFile('avatars', base64_decode($image));

Per the docs, you're going to want to use $request->file('avatar') to access the file.

Then, you can do store('avatars') to store it in your default storage location.

In short:

$path = $request->file('avatar')->store('avatars');
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • Does store put it in s3? – John Freedom May 26 '18 at 01:28
  • I don't think that for base64 to images though – John Freedom May 26 '18 at 01:29
  • @JohnFreedom `store()` does the same thing `Storage::putFile` does. It'll use whatever the "default" storage method in `config/filesystems.php` is set to. You don't need any base64ing here. – ceejayoz May 26 '18 at 01:31
  • I am not using php for the frontend I am using due for the frontend so the only way to send the image to my laravel backend is base64 I am passing it with json using axios – John Freedom May 26 '18 at 01:33
  • @JohnFreedom It's entirely possible to send the actual file unencoded with Axios. See the accepted answer at https://stackoverflow.com/questions/43013858/ajax-post-a-file-from-a-form-with-axios. Base64 encoding the file adds 33% to the file size, so your users will even thank you for it. – ceejayoz May 26 '18 at 01:38
  • I used this to make my component. I am still not getting how not to use base 64 – John Freedom May 26 '18 at 01:53