使用保存方法查询数据库时出现问题

Problem with query on the database with save method

我有一个以前从未见过的奇怪问题 Laravel 9。为什么它会在 UserID 列而不是 user_id?

列中添加数据

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'UserId' in 'field list' insert into translation_jobs (UserId, JobName, TranslFile, updated_at, created_at) values (1, lalla, import/orNm2yVkussVFZqoG1NIyJZacnrofAfhymFFzWYs.xlsx, 2022-03-11 09:32:18, 2022-03-11 09:32:18)

型号

class TranslationJobs extends Model
{
    protected $fillable = [
        'UserId',
        'JobName',
        'FileName',
        'DocumentType',
        'RefLang',
        'TranslLanguages',
        'TranslNumber',
        'TranslDone',
        ];
}

迁移

public function up()
{
    Schema::create('translation_jobs', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('user_id');
        $table->string('job_name');
        $table->string('file_name');
        $table->integer('document_type')->nullable();
        $table->string('ref_lang')->nullable();
        $table->string('transl_languages')->nullable();
        $table->bigInteger('transl_number')->nullable();
        $table->string('transl_done')->nullable();
        $table->timestamps();
        $table->foreign('user_id')
        ->references('id')
        ->on('users')
        ->onDelete('cascade');
    });
}
  

Controller 我创建了一个新对象,并保存它:

$NewTranslationJob = new TranslationJobs();
$NewTranslationJob->UserID = auth()->id();
$NewTranslationJob->JobName = $request->name;
$NewTranslationJob->FileName = $ImportedFile;
$NewTranslationJob->save();

你的模型应该是这样的

class TranslationJobs extends Model
{
    protected $guarded = ['id']; // removed fillable
}

您应该在模型中使用迁移列名称。不是 UserId,而是 user_id