0

I created 2 classes called UserStoreRequest and UserAuthController in my laravel project. UserStoreRequest is a FormRequest to Validate the Request coming from the user. Once I send parameters, I just want to access these inside the Register method, but I get an empty array as response, instead of parameters sent.

UserAuthController:

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\UserStoreRequest;
use App\Models\User;

class UserAuthController extends Controller
{
    public function register(UserStoreRequest $request)
    {
        dd($request->all());

        /*  $data = $request->validate([
            'name' => 'required|max:255',
            'email' => 'required|email|unique:users',
            'password' => 'required|confirmed'
        ]);

        $data['password'] = bcrypt($request->password);

        $user = User::create($data);

        $token = $user->createToken('API Token')->accessToken;

        return response(['user' => $user, 'token' => $token]);*/
    }
}

UserStoreRequest:

namespace App\Http\Requests;

use Illuminate\Http\Request;

class UserStoreRequest extends Request
{
    /**
     * 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 [
            //
        ];
    }
}

Below I put the route used in the API class to send parameters:

Route::post('/register', 'App\Http\Controllers\Auth\UserAuthController@register');
1
  • 1
    I have never tested what happens if you have an empty rules array. I am not sure if that is your problem or not... So, doing dd($request->all()) returns an empty array, right ? Commented Jun 10, 2021 at 2:48

1 Answer 1

2

It looks like you're trying to use a Form Request. To do so, make sure you extend the proper parent class.

// Incorrect
use Illuminate\Http\Request;

class UserStoreRequest extends Request { /* ... */ }

// Correct
use Illuminate\Foundation\Http\FormRequest;

class UserStoreRequest extends FormRequest { /* ... */ }

The Laravel dependency injection container is able to instantiate a new instance of your existing Request, which is why you don't receive any errors and an instance of your request is available to the controller. However, Laravel doesn't know to populate it with the actual request data. That's where FormRequest comes in.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for response.If I go with FormRequest.I could not use the $validator = Validator::make($request->all(), $request->rules(), $request->messages());
Usually when using a Form Request, you don't use the validator manually. It is done automatically by Laravel using the rules() method to retrieve the validation rules. laravel.com/docs/8.x/validation#form-request-validation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.