Шаг: 1:
Добавить дополнительные поля в таблицу миграции пользователей
Вы уже сделали
Шаг: 2
Добавьте файлы к заполняемой модели User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
/**
* @property integer $id
* @property string $name
* @property string $surname
* @property string $email
* @property string $email_verified_at
* @property string $password
* @property integer $counter
* @property string $url_address
* @property string $ip
* @property string $date_of_registration
* @property integer $company_id
* @property boolean $isCompany
* @property boolean $isMailing
* @property string $note
* @property string $nip1
* @property string $business1
* @property string $phone1
* @property string $street1
* @property string $number1
* @property string $postal_code1
* @property string $city1
* @property integer $country_id1
* @property integer $provincial_id1
* @property string $nip2
* @property string $business2
* @property string $phone2
* @property string $street2
* @property string $number2
* @property string $postal_code2
* @property string $city2
* @property integer $country_id2
* @property integer $provincial_id2
* @property string $nip3
* @property string $business3
* @property string $phone3
* @property string $street3
* @property string $number3
* @property string $postal_code3
* @property string $city3
* @property integer $country_id3
* @property integer $provincial_id3
* @property float $cash
* @property string $remember_token
* @property string $created_at
* @property string $updated_at
*/
class User extends Authenticatable
{
use Notifiable;
/**
* @var array
*/
protected $fillable = ['name', 'surname', 'email', 'email_verified_at', 'password', 'counter', 'url_address', 'ip', 'date_of_registration', 'company_id', 'isCompany', 'isMailing', 'note', 'nip1', 'business1', 'phone1', 'street1', 'number1', 'postal_code1', 'city1', 'country_id1', 'provincial_id1', 'nip2', 'business2', 'phone2', 'street2', 'number2', 'postal_code2', 'city2', 'country_id2', 'provincial_id2', 'nip3', 'business3', 'phone3', 'street3', 'number3', 'postal_code3', 'city3', 'country_id3', 'provincial_id3', 'cash', 'remember_token', 'created_at', 'updated_at'];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
Шаг: 3
Добавить поля в регистрационную форму можно в projectname\resources\views\auth\register.blade.php
Добавить необходимые поля в форму
Шаг: 4
Теперь логическая часть
Откройте файл project\app\Http\Controllers\Auth\RegisterController.php
Теперь вы найдете валидатор и метод создания
Добавьте ваш метод к RegisterController.php
function generateSeoUrl(string $string): string
{
$string = preg_replace("/ +/", "-", trim($string));
$string = mb_strtolower(preg_replace('/[^A-Za-z0-9-]+/', '', $string), 'UTF-8');
return $string;
}
Добавьте логику проверки в validator
метод
/**
* 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)
{
$newArray = [
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'ip' => RequestFacade::ip(),
'date_of_registration' => now(),
'url_address' => $this->generateSeoUrl($data['email']),
];
return User::create($newArray);
}
Наконец, Ваш RegisterController будет выглядеть как
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request as HttpRequest;
use Illuminate\Support\Facades\Request as RequestFacade;
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 = '/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)
{
$newArray = [
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'ip' => RequestFacade::ip(),
'date_of_registration' => now(),
'url_address' => $this->generateSeoUrl($data['email']),
];
return User::create($newArray);
}
function generateSeoUrl(string $string): string
{
$string = preg_replace("/ +/", "-", trim($string));
$string = mb_strtolower(preg_replace('/[^A-Za-z0-9-]+/', '', $string), 'UTF-8');
return $string;
}
}
Пожалуйста, прокомментируйте, если есть какие-либо проблемы