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...
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();
}
Blade view
<div > @if (auth()->user()->detail_user()->first()->photo) <img src="{{ asset('storage/assets/photo/'. auth()->user()->detail_user()->first()->photo) }}" alt="photo profile" > @else <span > <svg 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" >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" onclick="return confirm('Are you sure want to delete your photo?')"> Delete </a> </div> @if ($errors->has('photo')) <p >{{ $errors->first('photo') }}</p> @endif </div>
CodePudding user response:
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
}
CodePudding user response:
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'];
CodePudding user response:
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());