ちょちょいと書いていきます。
usersテーブルにカラムを追加。
これは今まで通りやればOK。
今回は下記のように既存のcreate_users_table.php
のマイグレーションファイルにuser_original_id
というカラムを追加したとする。
database/migrations/2014_10_12_000000_create_users_table.php
<?php
~省略~
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('user_original_id')->unique();
$table->string('name');
~省略~
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
あとは実行すればOK。
php artisan migrate
もちろん別にマイグレーションファイルを作成して、カラムを追加してもOK。
Userモデルの修正
続いては、Userモデルの$fillable
の部分に今追加したカラム名を追加します。
User.php
protected $fillable = [
'user_original_id',//これ
'name',
'email',
'password',
];
バリデーションを追加
続いて、app\Actions\Fortify\CreateNewUser.php
にバリデーションなどを追加する記述をします。
app\Actions\Fortify\CreateNewUser.php
class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;
public function create(array $input)
{
Validator::make($input, [
~省略~
'user_original_id' => ['required', 'max:20', 'unique:users'],
~省略~
])->validate();
return User::create([
'user_original_id'=>$input['user_original_id'],
~省略~
]);
}
}
inputフィールドの追加
あとは、新規作成時のviewファイルにinputフィールドを追加すればOKです。
name属性
を先ほど作成したカラム名と同じにする必要があるので注意してください。
またデフォルトのユーザー新規作成ページはresources\views\auth\register.blade.php
ですので、そちらに追加しましょう。
resources\views\auth\register.blade.php
<div class="mt-4">
<x-jet-label for="user_original_id" value="share ID (半角英数字)" />
<x-jet-input id="user_original_id" class="block mt-1 w-full" type="text" name="user_original_id" :value="old('user_original_id')" required autocomplete="user_original_name" />
<small class="text-gray-500">*登録後の変更は出来ませんので、ご注意ください。</small>
</div>
(インデントバグってますが、面倒なのでこのままでm(_ _)m)
プロフィール編集時にも反映させる。
app\Actions\Fortify\UpdateUserProfileInformation.php
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'user_original_id' => ['required', 'max:20', 'unique:users'], //ここ
'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
'photo' => ['nullable', 'image', 'max:1024'],
])->validateWithBag('updateProfileInformation');
~省略~
if ($input['email'] !== $user->email &&
$user instanceof MustVerifyEmail) {
$this->updateVerifiedUser($user, $input);
} else {
$user->forceFill([
'name' => $input['name'],
'user_original_id'=>$input['user_original_id'], //ここ
'email' => $input['email'],
])->save();
}
編集ページに下記も追加。
resources\views\profile\update-profile-information-form.blade.php
<!-- 自己紹介 -->
<div class="col-span-6 sm:col-span-4">
<x-jet-label for="introduction" value="introduction') }}" />
<x-jet-input id="introduction" type="text" class="mt-1 block w-full" wire:model.defer="state.introduction" autocomplete="introduction" />
<x-jet-input-error for="introduction" class="mt-2" />
</div>
これでOK。