Reputation: 77
// Creating account
public function store()
{
$this->validate(request(),[
all inputs validated...
including ‘photo’ => ‘file|image|max:500’
]);
if(request()->hasFile(‘photo’)){
$path = request()->file(‘photo’)->store(‘profile’, ‘public’);
}
else{
$path = ‘avatar.jpg’;
}
$user = User::create(
[
// all inputs requested...
including ‘photo’ => $path,
]);
// I log user in and redirect to intended destination
// Success...was able to output profile photo and other things
}
// Now Update account is my problem
public function update(Request $request, User $user-profile)
{
$this->validate($request, [
all inputs validated..
including ‘photo’ => ‘file|image|max:1500
]);
// Genesis of my problem
if (request()->hasFile(‘photo’)){
$path = request()->file(‘photo’)->store(‘profile’, ‘public’);
}
else{
// Please what should I do here....
$user-profile->update(
[
// all inputs requested...
including ‘photo’ => ???
]);
}
The “else” condition of the update method is where my problem lies. Reason: if a user already has a profile photo, if I do the same “else” as during account creation, the ‘avatar.jpg’ then overwrites their initial photo which is not right. Please what can I do to solve this? So when user registers with photo, it’s loaded else avatar is loaded. But when user updates, what do I do? Thanks in advance.
Upvotes: 0
Views: 2966
Reputation: 34718
Do same like as store :
if (request()->hasFile(‘photo’)){
$path = request()->file(‘photo’)->store(‘profile’, ‘public’);
} else {
$path = ‘avatar.jpg’;
}
Upvotes: 1
Reputation: 34
i dont think you need the else.. if user has uploaded photo then you will need to overwrite users path in the db (which you are doing in the if) otherwise you dont need to and the save() call shouldnt even touch the path field in db
i am assuming your code for creation isnt being triggered somehow when you do the update.
Upvotes: 0