Reputation: 31
I don't know if this is possible to do. I have created a Laravel 6 project and adding auth. Now I want the program to generate a username during the registration process. The user does not have any input field to write their username. The program should generate that automatically. For example, user's name is "test" and the username can be test12345678. Basically, it will be the user's name + 8 random numbers. This is the register.blade.php
<body>
<br>
<br>
<div class="container">
<div class="row">
<div class="col-sm"></div>
<div class="col-sm">
<div class="text-center">
<img src="logo.png" alt="twitter-logo" width="30px">
</div>
<br><br>
<div class="text-start">
</div>
</div>
<div class="col-sm">
<form method="POST" action="{{ route('register') }}">
<button type="submit" class="btn btn-primary" id="submitBtn">
{{ __('Next') }}
</button>
</div>
</div>
</div>
<div class="container">
<div class="text-start">
<div class="row">
<div class="col"></div>
@csrf
<br>
<div class="col-6">
<div class="text">
<h3>Create your account</h3>
</div>
<br>
<input class="design" id="name" type="text" @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus placeholder="Name">
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
<br><br>
<input class="design" id="email" type="email" @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" placeholder="Email">
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
<br><br>
<input class="design" id="password" type="password" @error('password') is-invalid @enderror" name="password" required autocomplete="new-password" placeholder="Password">
@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
<br><br>
<input class="design" id="password-confirm" type="password" name="password_confirmation" required autocomplete="new-password" placeholder="Confirm Password">
</form>
</div>
<div class="col"></div>
</div>
</div>
</div>
<br>
</body>
And this is the registerController.php:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'avatar' => 'default.png',
]);
}
}
I have also added a new column in the users table named "username"
Upvotes: 0
Views: 3796
Reputation: 10210
You could add the following to the User::create
call in the create
method on the RegisterController
.
'username' => $data['name'] . rand(pow(10, 8 - 1), pow(10, 8) -1);
That will take the name they provided and append 8 random digits.
Don't forget to add username
to the $fillable
property on the User
model.
protected $fillable = ['name', 'email', 'password', 'avatar', 'username'];
Upvotes: 2
Reputation: 1111
You can do that in RegisterController
as below
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'username' => $data['name'] . str_pad(mt_rand(1,99999999),8,'0',STR_PAD_LEFT)
'avatar' => 'default.png',
]);
}
Upvotes: 0