Quick Edit: Ok I'm one step forward, I realized that $this->user is the currently being edited users' name. So I changed my rules method to this:
public function rules()
{
$user = User::where('name', '=', $this->user)->first();
return [
'username' => ['required', Rule::unique('users', 'name')->ignore($user->id)],
'email' => ['required', 'email', Rule::unique('users', 'email')->ignore($user->id)],
];
}
I don't need to use the session anymore. It's still working as far as I can tell. However, would you do it like that?
based on your reply, I tried to view $this->user in my case it contains user id so I do not need to do this query User::where('name', '=', $this->user)->first();
I am not sure how id end up on $this->user when I check other models I get same result for example model Task I get $this->task that contains id, however now unique ignore current user works for email field
thank you
this is my code
<?php
namespace App\Http\Requests\User;
use Illuminate\Foundation\Http\FormRequest;
class UpdateUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->user()->can('user-update');
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email|unique:users,email,'.$this->user,
'address' => '',
'work_number' => 'numeric',
'personal_number' => 'numeric',
'password' => 'sometimes',
'password_confirmation' => 'sometimes',
'image_path' => 'mimes:jpeg,jpg,png,gif|max:100000',
'roles' => 'required',
'departments' => ''
];
}
}
controller action
/**
* @param $id
* @param UpdateUserRequest $request
* @return mixed
*/
public function update($id, UpdateUserRequest $request)
{
$this->users->update($id, $request);
Session()->flash('flash_message', 'User successfully updated');
return redirect()->back();
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community