Sergey S.
Sergey S.

Reputation: 157

Laravel: error "Undefined variable" when i validate form request with using custom Request class

In Laravel (5.8) controller, i try to make update() function for my User model. I validate data with using my own class UpdateRequest. When i put variable $user in this class, i have error Undefined variable: user.

<?php

namespace App\Http\Requests\Users;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateRequest extends FormRequest
{

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name'  => 'required|string|alpha_dash|max:255|min:6',
            'email' => ['required',
                        'string',
                        'email',
                        'max:255',
                        Rule::unique('users')->ignore($user->id)],
         ];
    }
}


public function update(User $user, UpdateRequest $request)
    {
        $user->update($request->only(['name', 'email']));
        return redirect()->route('users.index');
    }

But if I use validate function in controller update() method, all works fine.

public function update(User $user, Request $request)
    {
     $this->validate($request, [
           'name'  => 'required|string|alpha_dash|max:255|min:6',
           'email' => 'required|string|email|max:255|unique:users,id,' . $user->id,
        ]);

        $user->update($request->only(['name', 'email']));

        return redirect()->route('users.index');
    }

Upvotes: 1

Views: 6023

Answers (3)

Suman Raj
Suman Raj

Reputation: 1

User class instance is missing in UpdateRequest class constructor or you can try with $this->user->id. It may help you.

Upvotes: 0

nakov
nakov

Reputation: 14278

In your custom request class, you don't have the $user initialized and you try to use it, while in the controller method the $user is passed as a parameter.

Note $this->user in the Request returns the currently authenticated user, so make sure that you always want to use his ID, instead of an ID of the passed in user, hence the reason I am using request('user') to get the user id from the URL.

So try this instead:

public function rules()
{
    return [
            'name'  => 'required|string|alpha_dash|max:255|min:6',
            'email' => ['required',
                        'string',
                        'email',
                        'max:255',
                        Rule::unique('users')->ignore(request('user'))],
         ];
}

Upvotes: 2

Rohit Mittal
Rohit Mittal

Reputation: 2104

You need to change $user->id to $this->user->id and it should work properly. Check below:

return [
            'name'  => 'required|string|alpha_dash|max:255|min:6',
            'email' => ['required',
                        'string',
                        'email',
                        'max:255',
                        Rule::unique('users')->ignore($this->user->id)],
         ];

Hope it helps you!!

Upvotes: 1

Related Questions