Laravel Authentication (Practical Implementation)

Dikshant Rajput
3 min readMay 23, 2021

In previous blog, I have discussed about how to install Laravel bootstrap auth and in this blog, I will tell this with the help of code. If you have not seen my previous blog, do have a look on it here:

After installing auth scaffolding, you will get a new folder in app\Http\Controllers named Auth

Auth folder structure

As you can see there are multiple Controllers files with logic for handling your users. The logic behind these files are self explanatory as their name.

Try to register a user by going to /register url and you will notice that by default name, email, password and confirm password field. Whenever you register a user you will automatically login.

The frontend(view) files for register, login, and some passwords related files are inside the resources\views\auth folder.

In many conditions, you may also have to add some extra fields to the user . You can easily do it by going to your register.blade.php file and adding a new input as per your requirements. In this blog I will be adding a mobile number field.

<div class="form-group row"><label for="mobile" class="col-md-4 col-form-label text-md-right">{{ __('Mobile Number') }}</label><div class="col-md-6"><input id="mobile" type="mobile" class="form-control @error('mobile') is-invalid @enderror" name="mobile" value="{{ old('mobile') }}" required autocomplete="mobile">@error('mobile')<span class="invalid-feedback" role="alert"><strong>{{ $message }}</strong></span>@enderror</div></div>

After adding it your frontend will look like this

Add the mobile field in your database by creating a migration by running

php artisan make:migration add_mobile_field_to_users_table

A new migration file will be created in database/migrations folder

Add this to up method of this file:

Schema::table('users', function (Blueprint $table) {$table->string('mobile',10)->nullable();});

Then run,

php artisan migrate

Then add ‘mobile’ to the fillable array of User model

protected $fillable = [    'name',    'email',    'password',    'mobile'];

Now you have to change the logic part i.e. controller file. Open RegisterController.php and edit the validator function.

'mobile'=>['required','string','digits:10']

Then edit the create method where the user is actually created, Add this line to User::create function

'mobile'=>$data['mobile']

Now go to your /register url and try to register the user with mobile number field filled

After successfully registering you will be login to the application . To access this field go to the home.blade.php file and write this code anywhere in the file according to the view you want.

{{ __('Mobile : ') . auth()->user()->mobile }}

Congratulations!!! You have successfully created a new field in db . Now you can add any field or can remove any field according to your requirement.

You can find the above code on github here:

Thank you for reading. If you like the post, please hit that clap icon and follow me for more blogs on Laravel basics…..

--

--