I am creating profile edit function in my laravel project.
I want to update profile image. When I upload a new image, I can see a new image in profile/index.blade.php.
But when I didn't upload an image, I could not see any image in profile/index.blade.php. And I got an error which says
Profile edit page is profile/create.blade.php. And when I hit save button, I can redirect to a profile page, profile/index.blade.php.
What I want to accomplish is,
when I upload a new image, I want to show a new profile on index.blade.php. And if I don't upload an image, I want to redirect to index.blade.php and show a previous profile image.
I have profile folder in my Storage. I have already done php artisan storage:link.
UserController.php
public function store(Request $request, Profile $profile)
{
$user_id = auth()->user()->id;
$data = $request->only(['image']);
if($request->hasFile('image')){
//upload it
$image = $request->image->store('profile');
//delete old image
Storage::delete($profile->image);
$data['image'] = $image;
} else {
$image = $request->image->store('profile');
}
Profile::updateOrCreate(
['user_id' => $user_id],
[
'image' => $image,
'gender' => request('gender'),
'country' => request('country'),
'bod' => request('bod'),
'instagram' => request('instagram'),
'description' => request('description'),
]);
//redirect user's profile page
return redirect()->route('profile.index');
}
create.blade.php
<form action="" method="POST" enctype="multipart/form-data" name="image">
@csrf
<div class="preview">
<input type="file" id="file" accept="image/*" name="image">
<label for="file">
Change Image
</label>
</div>
Save
index.blade.php
<div class="person">
@if(empty(Auth::user()->profile->image))
<img src="" >
@else
<img src="" >
@endif
</div>
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire