0

I'm trying to build a multiple authentification in laravel with different tables (2 tables) for admin and user. The problème is that the registration and login forms work only with default auth login/register.

I've tried some examples form web tutorials but it didn't work.

HomeController.php:

public function __construct() {
    $this->middleware('auth');
}

public function index() {
    return view('home');
}

I have added createAdmin function in "Auth/RegisterController.php":

protected function createAdmin(array $data)
{
    $this->validator($data->all())->validate();
    $admin = Admin::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);
    return redirect()->intended('login/admin');
}

I have changed email validation rules to:

'email' => ['required', 'string', 'email', 'max:255', 'unique:users'|'unique:admins']

And the route (web.php) is:

Route::post('/register/admin', 'Auth\RegisterController@createAdmin');

When I fill admin register credentials and click register button I get this message:

Symfony\Component\Debug\Exception\FatalThrowableError Too few arguments to function App\Http\Controllers\Auth\RegisterController::createAdmin(), 0 passed and exactly 1 expected

1 Answer 1

1

The error is coming from the array $data parameter in your createAdmin() controller method.

Usually, you want to use one of two types of parameters in your controller methods: route parameters or injected dependencies. The $data parameter isn't matching either of those, so Laravel doesn't know to provide it.

If you'd like to access the request (POST) data in the controller, you can either ask for an instance of Illuminate\Http\Request as a parameter:

// Import it at the top of your PHP file
use Illuminate\Http\Request;

// Then your updated method:
public function createAdmin(Request $request)
{
    $data = $request->all();

    // ...
}

Or, use the request() helper directly:

public function createAdmin()
{
    $data = request()->all();

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

Comments

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.