erp/app/Http/Controllers/Auth/RegisterController.php

80 lines
2.1 KiB
PHP
Raw Normal View History

2022-07-23 17:07:13 +08:00
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
2022-07-23 17:07:13 +08:00
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
2022-07-27 19:06:16 +08:00
use Faker\Generator as Faker;
use Illuminate\Support\Str;
2022-07-23 17:07:13 +08:00
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, [
2022-07-27 19:06:16 +08:00
'name' => ['required', 'string', 'unique:users', 'max:255'],
'email' => ['string', 'email', 'max:255', 'unique:users'],
2022-07-23 17:07:13 +08:00
'password' => ['required', 'string', 'min:8', 'confirmed'],
2022-07-27 19:06:16 +08:00
'role_id' => ['required', 'numeric', 'exists:roles,id'],
2022-07-23 17:07:13 +08:00
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
2022-07-27 19:06:16 +08:00
* @return User
2022-07-23 17:07:13 +08:00
*/
protected function create(array $data)
{
2022-07-27 19:06:16 +08:00
$faker = new Faker();
2022-07-23 17:07:13 +08:00
return User::create([
'name' => $data['name'],
2022-07-27 19:06:16 +08:00
'email' => $data['email'] ?? $faker->unique()->safeEmail,
2022-07-23 17:07:13 +08:00
'password' => Hash::make($data['password']),
2022-07-27 19:06:16 +08:00
'api_token' => Str::random(60),
2022-07-23 17:07:13 +08:00
]);
}
}