0

i am using laravel 8 framework but can't seem to update my profile image. It always goes to fallback image when i am trying to update the image. enter image description here And here is my code

profile.blade.php

<div class="card-body">
    <form action="{{ route('profile.update') }}" method="POST" enctype="multipart/form-data">
        @csrf
        @method('patch')

        <div class="form-group">
            <label for="image">Profile Image <span class="text-danger">*</span></label>
            <img style="width: 100px;height: 100px;" class="d-block mx-auto img-thumbnail img-fluid rounded-circle mb-2" src="{{ auth()->user()->getFirstMediaUrl('avatars') }}" alt="Profile Image">
            <input id="image" type="file" name="image" data-max-file-size="500KB">
        </div>

ProfileController.php :

   public function update(Request $request) {
        if ($request->has('image')) {
            if ($request->has('image')) {
                $tempFile = Upload::where('folder', $request->image)->first();

                if (auth()->user()->getFirstMedia('avatars')) {
                    auth()->user()->getFirstMedia('avatars')->delete();
                }

                if ($tempFile) {
                    auth()->user()->addMedia(Storage::path('temp/' . $request->image . '/' . $tempFile->filename))->toMediaCollection('avatars');

                    Storage::deleteDirectory('temp/' . $request->image);
                    $tempFile->delete();
                }
            }
        }

        toast('Profile Updated!', 'success');

        return back();
    }

web.php

//User Profile
    Route::patch('/user/profile', 'ProfileController@update')->name('profile.update');

User.php

public function registerMediaCollections(): void
    {
        $this->addMediaCollection('avatars')
            ->useFallbackUrl('https://www.gravatar.com/avatar/');
    }

Strangely enough i have image in product tabs and it's working fine perfectly, it just error in profile yet it didn't show any error and i am so clueless about this.

I want to make that the image i uploaded is shown in mediacollection named 'avatars'. Yet it always use the fallback image, what to do and how to debug that? i am a beginner sorry.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • It says 'profile updated, but has it really uploaded about image? You can check by using `dd();` multiple times. First check is `$request->has('image') triple check that one first – UnderDog Jul 15 '23 at 16:56
  • I did your suggesstion, the result are always 'true', what to do next? – M. Febriandi Muslim Jul 16 '23 at 02:53
  • Keep dd-ing inside that if-statement. You have some variables that you can get the details from with dd – UnderDog Jul 16 '23 at 04:11
  • Everygthing is going fine. The image uploaded successfully and got placed at the temp folders. It just that, when retrieving to to show it on the web. It always choose the fallback image instead of image in temp folder. – M. Febriandi Muslim Jul 18 '23 at 13:32

0 Answers0