Laravel how to make registration

Laravel: Learn how to easily create a registration system with an example.

Creating a Registration Form in Laravel

Laravel makes creating a user registration form a breeze. To create a registration form, you will need to use the make:auth command. This command will generate all of the necessary views and routes for the authentication system, including the registration form.

To get started, open a terminal window and navigate to your project's root directory. From there, you can run the following command to generate the authentication views and routes:

php artisan make:auth

This command will generate all of the necessary views and routes for the authentication system, including the registration form. You can find the registration form in the resources/views/auth/register.blade.php file. The form will look something like this:

<form method="POST" action="{{ route('register') }}">
    @csrf

    <div class="form-group row">
        <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

        <div class="col-md-6">
            <input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>

            @error('name')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        </div>
    </div>

    <div class="form-group row">
        <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

        <div class="col-md-6">
            <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">

            @error('email')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        </div>
    </div>

    <div class="form-group row">
        <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

        <div class="col-md-6">
            <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">

            @error('password')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        </div>
    </div>

    <div class="form-group row">
        <label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>

        <div class="col-md-6">
            <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
        </div>
    </div>

    <div class="form-group row mb-0">
        <div class="col-md-6 offset-md-4">
            <button type="submit" class="btn btn-primary">
                {{ __('Register') }}
            </button>
        </div>
    </div>
</form>

As you can see, the form contains fields for the user's name, email address, and password. The form also includes a token field and an error-handling mechanism, both of which will be covered in more detail in a later section. For now, let's take a look at how we can customize the form.

The first thing you may want to do is add additional fields to the form. You can do this by simply adding additional <input> elements to the form. For example, if you wanted to add a field for the user's age, you could do so by adding the following code:

<div class="form-group row">
    <label for="age" class="col-md-4 col-form-label text-md-right">Age</label>

    <div class="col-md-6">
        <input id="age" type="number" class="form-control" name="age">
    </div>
</div>

You may also want to add a "Confirm Password" field. This is a simple check to make sure that the user has entered the same password twice. To add this field, you can add the following code:

<div class="form-group row">
    <label for="password-confirm" class="col-md-4 col-form-label text-md-right">Confirm Password</label>

    <div class="col-md-6">
        <input id="password-confirm" type="password" class="form-control" name="password_confirmation">
    </div>
</div>

Once you have added all of the necessary fields to the form, you will need to add them to the validation rules. You can do this by adding the fields to the rules() method in the RegisterController class. For example, if we wanted to validate the age field, we would add the following line to the rules() method:

'age' => 'required|integer',

Finally, you will need to add the fields to the create() method in the RegisterController class. This is where the data from the form will be stored in the database. For example, if we wanted to store the age field, we would add the following line to the create() method:

'age' => $data['age'],

And that's all there is to it! With just a few lines of code, you can quickly and easily create a registration form in Laravel.

Answers (0)