user11082357
user11082357

Reputation:

How to take User Id from Request(rules) for update?

Could I know how to take the user Id to Request an update? for example, when I updated the user and password. But, except email. At that time, It showed "the message that the email is already taken. When I searched for solutions, I found to solve with the user id. I know this question is asked many times. But, I didn't get any suitable answer for me. Could you help me, please?

This is my Controller Code

public function edit(Users $request,$id){ 
    $users=User::whereId($id)->firstorFail();
    $users->name         = $request->get('name');
    $users->email        = $request->get('email');
    $users->password     = Hash::make($request->get('password'));
    $users->role         = $request->get('role');
    $users->update();
    $request->session()->forget('editvalue');
    $userdata = User::paginate(4);
    // session()->flash('status', 'User has been successfully added.');
    return view('pages.auth.register', compact('userdata'))->with('status','User has been successfully added.');
}

This is my Request Form. I want to take id value in this. When I take value, it is showing the message that Trying to get property 'id' of non-object

public function rules() {
    return [
        'name'          => 'required', 'string', 'max:255',
        'email'         => 'sometimes','required', 'string', 'email', 'max:255', 'unique:users,'. $this->users->id,
        'password'      => 'required', 'string', 'min:8', 'confirmed',
        'role'          => 'required', 'string',
    ];
}

This is my web.php

Route::get('users/edit/{id}', 'UsersController@editscreen');
Route::post('users/edit/{id}', 'UsersController@edit');

Upvotes: 0

Views: 838

Answers (4)

Goku San
Goku San

Reputation: 81

I just encountered this problem and managed to solve by adding $this->id only.

public function rules()
{
     return [
        'name'  => ['required', 'string', 'max:255']
        'email' => ['required', 'string', 'unique:users,email,' . $this->id]
    ];
}

Upvotes: 0

user11082357
user11082357

Reputation:

I got with this.

    use Illuminate\Validation\Rule;//import Rule class 

    public function rules()
{
    return [
        'name'          => 'required', 'string', 'max:255',
        'email'         => ['sometimes','required', 'string', 'email', 'max:255',
        Rule::unique('users')->ignore($this->id),
    ],
        'password'      => 'required', 'string', 'min:8', 'confirmed',
        'role'          => 'required', 'string',
    ];
}

Upvotes: 0

Tom
Tom

Reputation: 16

I think you can't validate {id} in the request class but you can validate with regex in the route. (My Example is for laravel 8 but the principle remains the same)

Route::post('/users/edit/{id}', [UsersController::class, 'editscreen'])
        ->where('id', '[0-9]+');

Upvotes: 0

ibrahim-dogan
ibrahim-dogan

Reputation: 621

You should also put the column name to the rule, the pattern should be unique:table,column,except_id

Can you replace your RequestForm with this:

public function rules()
{
    return [
        'name'          => ['required', 'string', 'max:255'],
        'email'         => ['sometimes','required', 'string', 'email', 'max:255', 'unique:users,email,'. $this->users->id],
        'password'      => ['required', 'string', 'min:8', 'confirmed'],
        'role'          => ['required', 'string'],
    ];
}  }

Upvotes: 1

Related Questions