Level 2
I didn't use the default Auth, I used custom Auth.
I would really appreciate if someone can please help me. This has not been working since yesterday evening.
Here is my Form, Controller, Model and Route:
Having imported all necessary classes at the top of Controller, Model and Route...
Result: the form submitted without no issues but no feedback by session and values didn't change.
// My Form.....
<form action="/user/update-profile/{$userDetail->id}" method="POST" enctype="multipart/form-data">
@method('PATCH')
@csrf
<div>
<div class="form-row p-1">
<div class="col-sm-12 col-md-12 mb-3">
<label for="username-id">Username</label>
<input type="text" class="form-control" id="username-id" name="username" value="{{$userDetail->username}}" placeholder="Enter your username.." required autocomplete="username" autofocus>
@error('username')
<small class="is-invalid text-left alert alert-danger bg-light p-1">
{{ $message }}
</small>
@enderror
</div>
<div class="col-sm-12 col-md-12 mb-3">
<label for="name-id">Name</label>
<input type="text" class="form-control" id="name-id" name="name" value="{{$userDetail->name}}" placeholder="Enter your name.." required autocomplete="name" autofocus>
@error('name')
<small class="is-invalid text-left alert alert-danger bg-light p-1">
{{ $message }}
</small>
@enderror
</div>
<div class="col-sm-12 col-md-12 mb-3">
<label for="email-id">Email</label>
<input type="email" class="form-control" id="email-id" name="email" value="{{$userDetail->email}}" placeholder="Enter your email.." required autocomplete="email" autofocus>
@error('email')
<small class="is-invalid text-left alert alert-danger bg-light p-1">
{{ $message }}
</small>
@enderror
</div>
<div class="col-sm-12 col-md-12 mb-3">
<label for="password">Password</label>
<input type="password" class="form-control" id="password-id" name="password" placeholder="Enter your password.." required autocomplete="new-password">
@error('password')
<small class="is-invalid text-left alert alert-danger bg-light p-1">
{{ $message }}
</small>
@enderror
</div>
<div class="col-sm-12 col-md-12 mb-3">
<label for="password_confirmation-id">Repeat password</label>
<input type="password" class="form-control" id="password_confirmation-id" name="password_confirmation" placeholder="Repeat your password.." required autocomplete="new-password-confirm">
@error('password_confirmation')
<small class="is-invalid text-left alert alert-danger bg-light p-1">
{{ $message }}
</small>
@enderror
</div>
<div class="col-sm-12 col-md-6 mb-3">
<label for="user_avatar">Profile image</label>
<input type="file" class="form-control-file" id="user_avatar-id" name="user_avatar">
@error('user_avatar')
<small class="is-invalid text-left alert alert-danger bg-light p-1">
{{ $message }}
</small>
@enderror
</div>
<div class="col-sm-12 col-md-12 col-lg-12 col-xl-12 mb-3">
<label for="about">About</label>
<textarea class="form-control" id="about-id" rows="10" placeholder="Describe yourself...">{{$userDetail->about}}</textarea>
@error('about')
<small class="is-invalid text-left alert alert-danger bg-light p-1">
{{ $message }}
</small>
@enderror
</div>
<div class="col-sm-12 col-md-12 mb-3">
<button class="btn bg-light text-dark float-right" type="submit">Update</button>
</div>
</div>
</div>
</form>
// My Controller......
public function editUserProfile(User $userDetail){
return view('profile.edit', compact('userDetail'));
}
public function updateUserProfile(User $newUserDetail){
// dd(request()->all()); I did this and received the new/edited values
$userprofile = Auth::user();
$this->validate(request(), [
'username' => 'required|string|regex:/\w*/|max:255|unique:users,username,'.$userprofile->id,
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email,'.$userprofile->id,
'password' => 'required|string|min:8|confirmed',
'about' => 'nullable|regex:/\w*/|string|min:8',
'user_avatar' => 'required|file|image|max:500'
]);
// File upload
if (request()->hasFile('user_avatar')){
$path1 = request()->file('user_avatar')->store('Userprofile/upload1', 'public');
$path2 = request()->file('user_avatar')->store('Userprofile/upload2', 'public');
$profileImage1 = Image::make(public_path('storage/' . $path1))->fit(40,40);
$profileImage1->save();
$profileImage2 = Image::make(public_path('storage/' . $path2))->fit(60,60);
$profileImage2->save();
}
else{
$path1 = $newUserDetail->user_avatar;
$path2 = $newUserDetail->user_photo_60by60;
}
$newUserDetail->update([
'username' => strip_tags(request('username')),
'name' => strip_tags(request('name')),
'email' => strip_tags(request('email')),
'user_avatar' => strip_tags($path1),
'user_photo_60by60' => strip_tags($path2),
'password' => Hash::make(request('password')),
'about' => strip_tags(request('about')),
]);
session()->flash(
'feedback', 'Your profile was successfully updated.'
);
return redirect()->route('profile.settings');
}
// My Model
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username', 'name', 'email', 'password', 'user_avatar', 'user_photo_60by60', 'about',
];
}
// My Routes : Also, I imported all necessary classes at the top of the route file
Route::get('/user/{userprofile}/edit-profile', [UserProfileController::class, 'editUserProfile']);
Route::patch('/user/update-profile/{userprofile}', [UserProfileController::class, 'updateUserProfile']);
Turns out that the entire code was supposed to work but I didn't place things properly. The only thing I did to improve was tweak the updateUserProfile METHOD like below ...as opposed to the initial code...
public function updateUserProfile(){ // Removed the type-hinted User model instance
$userprofile = Auth::user(); // Used this very authenticated user object to update the new values below
$this->validate(request(), [
'username' => 'required|string|regex:/\w*/|max:255|unique:users,username,'.$userprofile->id,
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email,'.$userprofile->id,
'password' => 'required|string|min:8|confirmed',
'about' => 'nullable|regex:/\w*/|string|min:8',
'user_avatar' => 'required|file|image|max:500'
]);
// File upload
if (request()->hasFile('user_avatar')){
$path1 = request()->file('user_avatar')->store('Userprofile/upload1', 'public');
$path2 = request()->file('user_avatar')->store('Userprofile/upload2', 'public');
$profileImage1 = Image::make(public_path('storage/' . $path1))->fit(40,40);
$profileImage1->save();
$profileImage2 = Image::make(public_path('storage/' . $path2))->fit(60,60);
$profileImage2->save();
}
else{
$path1 = $userprofile->user_avatar;
$path2 = $userprofile->user_photo_60by60;
}
// This "$userprofile" variable is the authenticated user object I mentioned at the top which I am using here to update new values
$userprofile->update([
'username' => strip_tags(request('username')),
'name' => strip_tags(request('name')),
'email' => strip_tags(request('email')),
'user_avatar' => strip_tags($path1),
'user_photo_60by60' => strip_tags($path2),
'password' => Hash::make(request('password')),
'about' => strip_tags(request('about')),
]);
session()->flash(
'feedback', 'Your profile was successfully updated.'
);
// New values were received and updated and feedback was shown...
return redirect()->route('profile.settings');
}
Please or to participate in this conversation.