0
votes

I have been trying to get this api up and running and keep on experiencing this error when I test in Postman.

1. api.php
Route::group(['middleware' => ['api','cors']], function () {
     Route::post('auth/register', 'Auth\RegisterController@create');
});

2. RegisterController 
    protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}}
  1. Postman configuration set to Post, the body is set to raw and JSON(application/json) Below is the postman json code.

    { "name": "Walter White", "email": "[email protected]", "password": "testpassword" }

Below is the error Too few arguments to function App\Http\Controllers\Auth\RegisterController::create(), 0 passed and exactly 1 expected in file C:\xampp\examplestuff

3

3 Answers

1
votes

In order to fix your registration you should change your route definition to:

Route::group(['middleware' => ['api','cors']], function () {
    Route::post('auth/register', 'Auth\RegisterController@register');
});

I assume your RegisterController is using the trait RegistersUsers. This trait is providing the register method, which is using the RegisterController::create method to create the new user itself.

0
votes

Pass Request to your create function like:

protected function create(Request $request){...

and access your data like this:

$request->name
-1
votes

you have to do validation also plz check below answer

 protected function create(Request $request)
{
    $data = $request->json()->all();

    $validator =  Validator::make($data, [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
    ]);

    if ($validator->fails()) {
        foreach ($validator->messages()->getMessages() as $field_name => $message){
                $messages[] = $message[0];
        }
        $messages = $messages;
        $message = implode(',',$messages);
        $response = $messages;

        return $response;

    }else{

        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

}