laravel-null列

n53p2ov0  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(345)

我在我的位置加了两列 users table、viber和whatsapp,带有两个artisan命令 php artisan make:migration add_viber_to_users 以及 php artisan make:migration add_whatsapp_to_users . 以下是我的迁移:
viber迁移:

/**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->integer('viber')->after('status')->nullable();
        });
    }

whatsapp迁移:

/**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->integer('whatsapp')->after('viber')->nullable();
        });
    }

在我的心里 RegisterController :

/**
     * 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'],
            'telefon' => ['required', 'numeric', 'unique:users', 'min:6'],
            'drzava' => ['required'],
            'grad' => ['required'],
            'status' => ['required'],
            'viber' => ['required', 'numeric'],
            'whatsapp' => ['required', 'numeric'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'telefon' => $data['telefon'],
            'drzava' => $data['drzava'],
            'grad' => $data['grad'],
            'status' => $data['status'],
            'viber' => $data['viber'],
            'whatsapp' => $data['whatsapp'],
        ]);
    }

另外,这是我的表单选择字段 viber 以及 whatsapp :

<select class="form-control{{ $errors->has('viber') ? ' is-invalid' : '' }}" required name="viber" id="inputGroupSelect01">
   <option selected>Viber</option>
   <option value="1">Da</option>
   <option value="0">Ne</option>
</select>
@if($errors->has('viber'))
<div class="invalid-feedback">
   {{ $errors->first('viber') }}
</div>
@endif
<select class="form-control{{ $errors->has('whatsapp') ? ' is-invalid' : '' }}" required name="whatsapp" id="inputGroupSelect01">
   <option selected>Viber</option>
   <option value="1">Da</option>
   <option value="0">Ne</option>
</select>
@if($errors->has('whatsapp'))
<div class="invalid-feedback">
   {{ $errors->first('whatsapp') }}
</div>
@endif

这里有问题吗
我在这里的问题是每当我注册一个新用户,我选择两者 viber 以及 whatsapp 的字段 Da 所以价值是 1 在我的数据库中 NULL . 我看不出有什么问题?我怎样才能解决这个问题?

yb3bgrhw

yb3bgrhw1#

确保你跑步 php artisan migrate 已经迁移到你的table上了
确保将新列添加到 fillable 模型中的数组。

相关问题