Hi all.
I have created 2 controllers, one userController.php(to change user's data from the admin side) and one profileController.php(for the users to change his own data).
When I log in as an admin and update the profile of a user, it works.
When I log in as a user and update the owned profile of the user, I keep seeing this error:
The email has already been taken.
Both controllers use the same rule but the email rule is not working the same way when the email already exists on the profile user page.
userController.php(letting duplicate emails passing through)
/**
* @param \App\Http\Requests\UserUpdateRequest $request
* @param \App\Models\User $user
* @return \Illuminate\Http\Response
*/
public function update(UserUpdateRequest $request, User $user)
{
$this->authorize('update', $user);
$validated = $request->validated();
if (empty($validated['password'])) {
unset($validated['password']);
} else {
$validated['password'] = Hash::make($validated['password']);
}
if ($request->hasFile('avatar')) {
if ($user->avatar) {
Storage::delete($user->avatar);
}
$validated['avatar'] = $request->file('avatar')->store('public');
}
$user->update($validated);
$user->syncRoles($request->roles);
return redirect()
->route('users.edit', $user)
->withSuccess(__('crud.common.saved'));
}
profileController.php(NOT letting duplicate emails passing through)
/**
* @param UserUpdateRequest $request
* @param User $user
* @return mixed
* @throws AuthorizationException
*/
public function update(UserUpdateRequest $request, User $user)
{
$this->authorize('updateOwnProfile', $user);
$validated = $request->validated();
if (empty($validated['password'])) {
unset($validated['password']);
} else {
$validated['password'] = Hash::make($validated['password']);
}
if ($request->hasFile('avatar')) {
if ($user->avatar) {
Storage::delete($user->avatar);
}
$validated['avatar'] = $request->file('avatar')->store('public');
}
$role = Role::where('name', 'user')->first();
$user->update($validated);
$user->syncRoles($role->id);
return redirect()
->route('profile.edit', $user)
->withSuccess(__('crud.common.saved'));
}
Routes:
Route::resource('users', UserController::class);
Route::resource('profile', ProfileController::class);
Both using the same rule request file UserUpdateRequest.php:
<?php
namespace App\Http\Requests;
use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;
class UserUpdateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'first_name' => ['required', 'max:255', 'string'],
'last_name' => ['required', 'max:255', 'string'],
'email' => [
'required',
Rule::unique('users', 'email')->ignore($this->user),
'email',
],
'password' => ['nullable'],
'business_name' => ['nullable', 'max:255', 'string'],
'business_address_line_1' => ['nullable', 'max:255', 'string'],
'business_address_line_2' => ['nullable', 'max:255', 'string'],
'postcode' => ['nullable', 'max:255', 'string'],
'business_phone' => ['nullable', 'max:255', 'string'],
'vat_number' => ['nullable', 'max:255', 'string'],
'active' => ['required', 'max:255', 'string'],
'avatar' => ['nullable', 'image', 'max:1024'],
'roles' => 'array',
];
}
}
I used logger('test') before the rule return to see if both controllers were sending me to the same rule and both are correctly hitting the email rule but behaving differently.
I cannot work out why :-(
Any idea please?
Thanks