1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel8でユーザー作成時・編集時にカラムを追加する方法

Posted at

ちょちょいと書いていきます。

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。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?