$input * * @throws ValidationException */ public function create(array $input): User { Validator::make($input, [ 'name' => ['required', 'string', 'max:255'], 'email' => [ 'required', 'string', 'email', 'max:255', Rule::unique(User::class), // Never let public signup claim an existing (possibly not-yet- // provisioned) customer's email — ensureUser() would later link // that account and hand over the customer's portal. Rule::unique(Customer::class, 'email'), ], 'password' => $this->passwordRules(), ])->validate(); // Create the user and its linked customer atomically — a public signup is // a customer, and the portal (Billing::purchase, Settings, …) needs one. // Either both exist or neither, so a failure never orphans a user. return DB::transaction(function () use ($input) { $user = User::create([ 'name' => $input['name'], 'email' => $input['email'], 'password' => Hash::make($input['password']), ]); Customer::query()->create([ 'user_id' => $user->id, 'name' => $input['name'], 'email' => $input['email'], 'locale' => app()->getLocale(), 'status' => 'active', ]); return $user; }); } }