How to Register and Customize Users with Laravel 8 and Jetstream
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
⇨ Click here for the table of contents of Laravel articles
Writing about how to register and customize users with Laravel 8 using Jetstream.
Environment
"laravel/framework": "^8.54",
"laravel/jetstream": "^2.4",
What is Jetstream?
Jetstream builds functionalities like user registration and login for you.
In other words, when you run a command, various functionalities become available in your application.
Jetstream provides a beautifully designed application scaffold for Laravel, including login, user registration, email verification, two-factor authentication, session management, API support via Laravel Sanctum, and optional team management functionality. Jetstream is designed using TailwindCSS and can be chosen from either Livewire or Inertia.js-driven front-end scaffolds.
JetStream Japanese documentation
Installing Jetstream
Install using the command line.
composer require laravel/jetstream
php artisan jetstream:install livewire
Run commands for JS-related installations, asset compilation, and database migrations.
npm install && npm run watch
php artisan migrate
With this, Jetstream installation is complete.
You can confirm that functionalities like registration and login screens have been added to your routing.
Command:
php artisan route:list
From here, we will discuss how to customize the scaffold created using Jetstream.
Adding information to be saved in the users table
【CAUTION!】
You are directly editing migration files.
This is not a method for changes in services already in operation in production environments, so please be careful.
If you are not yet deployed to a production environment, there should be no issues.
Editing the migration file
The users table already includes the necessary minimum data for authentication and columns that you may "likely to use".
When you open:
database/migration/2014_10_12_000000_create_users_table.php
You will see various columns set up to be generated.
For instance, if you want to add "gender" as an input, let's try adding a "gender" column.
//Add this
//Place it not at the end but in a clear position in the order of columns
$table->string('gender')->nullable();
Overall (Migration file after adding the "gender" column)
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable();
$table->string('profile_photo_path', 2048)->nullable();
//Add this
//Place it not at the end but in a clear position in the order of columns
$table->string('gender')->nullable();
$table->timestamps();
});
After modifying the migration file, run the command
Run the following command:
php artisan migrate:fresh
Perform the migration but with the fresh option.
Remember that this is a command that can be run in a development environment, not in a production environment.
If you are already operating in a production environment, create a migration file to "add a column" and run the command
php artisan migrate
Since we are just getting started in this case, we are using this method.
Saving the "gender" on registration
Open App/Http/Controllers/Auth/RegisterController.php and
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'gender' => $data['gender'], //Added
'password' => Hash::make($data['password']),
]);
}
and add it.
Add to the User model
Add it to the whitelist.
App\Models\User.php or App\User.php
protected $fillable = [
'name', 'email', 'password', 'gender' //Added
];
Add an input form at registration
In resources/views/auth/register.blade.php
<input name="gender" />
Prepare an input tag (can also be a select tag) specifying "gender" as the name to allow input.
If you can enter and save successfully, you are good to go.
Login with a Login ID instead of email
First, change the settings in the config
In config/fortify.php, around line 49, you will find this:
'username' => 'email',
Change this to,
'username' => 'lid'
// 'username' => 'desired column name for login'
Edit the login page and registration page
Run the command
php artisan vendor:publish --tag=jetstream-views
Modify auth/login.blade.php
<input name="email" />
<input name="lid" />
Conclusion
Customizing registration processes seems to be somewhat different and somewhat unchanged from Laravel 6.
We will continue updating daily.
If you cannot find information about the customization you want to do, please contact us via Twitter.
Related articles
How to Save Various Data upon User Registration in Laravel
Popular articles
Deploying a PHP 7.4 + Laravel 6 Project to AWS EC2
Implementing Breadcrumbs in Laravel using laravel-breadcrumbs