首页 > 解决方案 > SQLSTATE [23000]:违反完整性约束:1048 列“tempat_lahir”不能为空

问题描述

我尝试从用户那里更新配置文件,所以首先我将为 tempat_lahir 进行插入

这是我的控制器

public function getUpdateProfile()
{
    return view('update_profile');
}

public function postUpdateProfile(Request $request)
{
    User::create([
        'tempat_lahir'  => $request->tempat_lahir,
   ]);

这是我的迁移

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->string('tempat_lahir');
    });
}

这是我的 User.php

protected $fillable = [
    'tempat_lahir'
];

这是我的 web.php

Route::get('/update_profile', 'AuthController@getUpdateProfile')->middleware('auth');
Route::get('/update_profile', 'AuthController@postUpdateProfile')->name('update_profile')->middleware('auth');

标签: phphtmlmysqllaravel

解决方案


你只需要编辑迁移文件


public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->string('tempat_lahir')->nullable();
    });
}

和新鲜的 laravel 迁移

或编辑tempat_lahir数据库中的列以添加nullable属性;

你也应该编辑RegisterController.php给数据表格来填充tempat_lahir


推荐阅读