1
votes

enter image description hereI have a following Controller in php laravel:

// .....

class RegisterController extends Controller
{
//...
//...
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
}

I use the following route:

Route::post('api/auth/register', 'Auth\RegisterController@create');

I am getting the following error: "Too few arguments to function App\Http\Controllers\Auth\RegisterController::create(), 0 passed and exactly 1 expected"

I need your help to pass Request parameters to my function (Form route properly)

2
try Auth\RegisterController@register instead of Auth\RegisterController@createSupun Praneeth
You are sending the data by route parameter or inside the request?porloscerros Ψ

2 Answers

1
votes

Try changing your method parameter to Request $request

to obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request class on your controller method. The incoming request instance will automatically be injected by the service container

and get the data from the request fields:

protected function create(Illuminate\Http\Request $request)
{
    return User::create([
        'name' => $request->name,
        'email' => $request->email,
        'password' => Hash::make($request->password),
    ]);
}

If you do not want to write all the Request namespace in the method parameter, add on the top of the file:

use Illuminate\Http\Request;

then, just use the name of the class:

protected function create(Request $request)
{
    //...
}
1
votes

You can do it in this way,

use Illuminate\Http\Request;

class RegisterController extends Controller
{
    protected function create(Request $request)
    {
        $data = $request->all();
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
}