Skip to content
Advertisement

Passing submit form to controller in laravel 7

I have a submit form which I want to send to my RegisterController and I get this error “”Too few arguments to function AppHttpControllersAuthRegisterController::create(), 0 passed and exactly 1 expected”” The create method demands an array.How to convert my request ot an array?

the form:

<form method="POST" action="{{ route('posting') }}">
                        @csrf....and so on

the routes:

Route::get('administration/register', function () {
    return view('vregister');
})->name('registration');


Route::post('/insert','AuthRegisterController@create')->name('posting');

method of RegisterController.php

  protected function create(array $data)
    {

        $user= User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);


        $role=Role::select('id')->where('name','Support')->first(); //??
        $user->roles()->attach($role);
        return $user;
    }

Advertisement

Answer

You are using wrong instance in your create method. When using forms, you should use Request class to actually send form data to your controller. The way you have it, no data is being sent to create() method, hence the error. Edit your create() method to this:

protected function create(Request $data)

And import Request class at the top:

use IlluminateHttpRequest;

Read more at the official documentation .

EDIT:

To redirect to a specific page after saving the data, change your return statement to this:

return redirect()->back();

This will return you back to the previous page. You can also add any route here, that you wish to redirect to:

return redirect()->rote('route.name');
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement