Home > Enterprise >  Problem with query on the database with save method
Problem with query on the database with save method

Time:03-11

I have a strange problem never seen before laravel 9

I have my model:

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

}

My migration:

    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');
    });
}
  

In my controller I create a new object, and saving it:

    $NewTranslationJob = new TranslationJobs();

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

But I get a strange error:

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)

Why is going to add data in the UserID column instead the user_id???

CodePudding user response:

Your model should be like this

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

You should use your migration column names inside your model. Not UserId but user_id

  • Related