Reputation: 170
I have two registration forms in Laravel using the same register controller. If a user registers as a customer, their id should be 1913xxxxxx where x is the value starting from 1. So it goes like 1913000001,1913000002,..... Similarly, if a user registers as a dealer, their id should be 1911xxxxxxx with an increment from 1 as well. They are both pumping data into the users
table so I am not sure how do I set the constraint on such a case.
RegisterController.php:
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\Users\User
*/
protected function create(array $data)
{
$user = User::create([
// Users table
'user_id'=>$data['1913xxxxxx' or '1911xxxxxx'],
'email' => $data['email'],
'password' => Hash::make($data['password'])
]);
$user->userInfo()->create([
'name' => $data['name'],
'NRIC' => $data['nric'], // Create NRIC field.
]);
$user->userAddresses()->create([
'address_1' => $data['homeaddress1'],
'address_2' => $data['homeaddress2'],
'address_3' => $data['homeaddress3'],
'zipcode' => $data['postcode'],
'shipping_address' => $data['shippingaddress']
]);
$user->userContacts()->create([
'mobile_num' => $data['number'],
'emergency_num' => $data['emergency']
]);
// check if dealer form is registered, assign dealer role or otherwise
if ($data['isDealerForm'] == 1) {
$user->assignRole('2');
} else {
$user->assignRole('1');
}
// $user->assignRole('1');
return $user;
}
}
Is this possible?
Upvotes: 0
Views: 1595
Reputation: 4574
Create another column track_id in your User Table where you will save 1913xxxxxx or 1911xxxxxx for customer or dealer and keep id as primary key auto incriminated. First create user and than save track_id with id like this
$user = User::create([
'email' => $data['email'],
'password' => Hash::make($data['password'])
]);
if ($data['isDealerForm'] == 1) {
$user->track_id = 191300000+$user->id;
$user->save();
$user->assignRole('2');
} else {
$user->track_id = 191100000+$user->id;
$user->save();
$user->assignRole('1');
}
i hope this will solve your problem.
update
create your migration like this
$table->unsignedBigInteger('track_id')->nullable();
when you are trying to create user it is looking for track_id value which is not there, with above migration if value is not there than it will be null but later you can update value after creating user.
Upvotes: 3