Image uploaded but database not updated (Update data with image in Laravel 8)

Viewed 75

I have make feature to update profile. The table is User HasOne DetailUser so every single user have 1 detail user. Everything works fine, except the photo update. When i was trying to update the photo, photo was succesfully update into storage in public but the database not updated. Maybe somebody could help me. Thanks...

Database DetailUser Public Folder

  1. Controller

    public function update(UpdateProfileRequest $request_profile, UpdateDetailUserRequest $request_detail_user) { $data_profile = $request_profile->all(); $data_detail_user = $request_detail_user->all();

     // save to user 
     $user = User::find(Auth::user()->id);
     $user = User::where('id', Auth::user()->id)->first(); 
     $user->name = $request_profile->input('name');
    
     // save to detail user 
     if($user->save()) {
         $detail_user = DetailUser::find($user->detail_user->id);
         $detail_user = DetailUser::where('id', $user->detail_user->id)->first();
    
         // delete old file from storage
         if (isset($data_detail_user['photo'])) {
             $data = 'storage/'. $detail_user['photo'];
             if (Storage::exists($data)) {
                 Storage::delete($data);
             } else {
                 Storage::delete('storage/app/public'. $detail_user['photo']);
             }
         }
    
         // store file to storage
         if (isset($data_detail_user['photo'])) {
             $data_detail_user['photo'] = $request_detail_user->file('photo')->store(
                 'assets/photo', 'public'
             );
         }
    
         $detail_user->role = $request_detail_user->input('role');
         $detail_user->contact_number = $request_detail_user->input('contact_number');
         $detail_user->biography = $request_detail_user->input('biography'); 
         $detail_user->save();
     }
    
     // save to experience
     $experience_user_id = ExperienceUser::where('detail_user_id', $detail_user['id'])->first();
     if (isset($experience_user_id)) {
    
         foreach ($data_profile['experience'] as $key => $value) {
             $experience_user = ExperienceUser::find($key);
             $experience_user->detail_user_id = $detail_user['id'];
             $experience_user->experience = $value;
             $experience_user->save();
         }
    
     } else {
    
         foreach ($data_profile['experience'] as $key => $value) {
             if (isset($value)) {
                 $experience_user = new ExperienceUser;
                 $experience_user->detail_user_id = $detail_user['id'];
                 $experience_user->experience = $value;
                 $experience_user->save();
             }
         }
    
     }
    
     toast()->success('Update has been success');
    
     return back();
    

    }

  2. Blade view

                                         <div class="flex items-center mt-1">
    
                                             @if (auth()->user()->detail_user()->first()->photo)
                                                 <img src="{{ asset('storage/assets/photo/'. auth()->user()->detail_user()->first()->photo) }}" alt="photo profile" class="w-16 h-16 rounded-full">
                                             @else
                                                 <span class="inline-block w-16 h-16 overflow-hidden bg-gray-100 rounded-full">
                                                     <svg class="w-full h-full text-gray-300" fill="currentColor" viewBox="0 0 24 24">
                                                         <path d="M24 20.993V24H0v-2.996A14.977 14.977 0 0112.004 15c4.904 0 9.26 2.354 11.996 5.993zM16.002 8.999a4 4 0 11-8 0 4 4 0 018 0z" />
                                                     </svg>
                                                 </span>
                                             @endif
    
                                             <label for="choose" class="px-3 py-2 ml-5 text-sm font-medium leading-4 text-gray-700 bg-white border border-gray-300 rounded-md shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500">Choose File</label>
                                             <input type="hidden" name="old_photo" value="{{ auth()->user()->detail_user()->first()->photo }}">    
                                             <input type="file" accept="image/*" id="choose" name="photo" hidden>
    
                                             <a href="{{ route('member.delete.photo.profile') }}" type="button" class="px-3 py-2 ml-5 text-sm font-medium leading-4 text-red-700 bg-transparent rounded-md hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500" onclick="return confirm('Are you sure want to delete your photo?')">
                                                 Delete
                                             </a>
                                         </div>
    
                                         @if ($errors->has('photo'))
                                             <p class="text-red-500 mb-3 text-sm">{{ $errors->first('photo') }}</p>
                                         @endif
    
                                     </div>
    
3 Answers

Are you getting an error? If not, you're probably not storing all the values you used in the UpdateDetailUserRequest.

Could you also share the request codes?


UPDATED:

Thank you for sharing the request. Since you are validating photo, role, contact_number and biography. I would recommend to change the following.

Change:

$detail_user->role = $request_detail_user->input('role');
$detail_user->contact_number = $request_detail_user- >input('contact_number');
$detail_user->biography = $request_detail_user->input('biography'); 
$detail_user->save();

To:

$detail_user->save($request_detail_user->validated());

Check your DetailUser model, is column photo fillable?

try to add this in your Model:

protected $fillable = ['id', 'photo', 'columnName', 'columnName'];

or

protected $guarded = ['id'];

You are not storing image value on database, update this:

if (isset($data_detail_user['photo'])) {
         $data_detail_user['photo'] = $request_detail_user->file('photo')->store(
             'assets/photo', 'public'
         );
    $detail_user->photo = $data_detail_user['photo']; //or some uniquely generated image name
}

Related