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');
}
index.blade.php
<div class="person">
@if(empty(Auth::user()->profile->image))
<img src="" >
@else
<img src="" >
@endif
</div>
create.blade.php
<div class="content">
<div class="userinfo">
<div class="profile">
</div>
<div class="image-preview" id="imagePreview">
@if(empty(Auth::user()->profile->image))
<img src="" id="image-preview__image">
@else
<img src="" id="preview">
@endif
</div>
<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>
<ul class="information">
<li>Gender :<br>
<div class="gender">
<select name="gender" id="" name="gender" >
<option class="option" value="@if(empty(Auth::user()->profile->gender))Select Gender
@else@endif" selected="selected">
@if(empty(Auth::user()->profile->gender))Select Gender
@else@endif
</option>
<option value="male" >male</option>
<option value="female" class="selected">female</option>
<option value="any">any</option>
</select>
</div>
</li>
<li>Country :<br>
<div class="country">
<select name="country" id="" name="country">
<option class="option" value="@if(empty(Auth::user()->profile->country))Select country
@else@endif" selected="selected">
@if(empty(Auth::user()->profile->country))Select country
@else@endif
</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
</select>
</div>
</li><br>
<li>Birthday :<br>
<input type="text" class="birthday" id="bod" name="bod"
value="@if(!empty(Auth::user()->profile->bod))@endif">
</li>
<li> Instagram :<br>
<input type="text" class="instagram" name="instagram"
value="@if(!empty(Auth::user()->profile->instagram))@endif">
</li><br>
<li>User Description :<br>
<textarea name="description" id="" cols="60" rows="10">@if(!empty(Auth::user()->profile->description))@endif</textarea></li>
<button type="submit" class="saveBtn">Save</button>
</ul>
</div>
via Chebli Mohamed

Aucun commentaire:
Enregistrer un commentaire