Search code examples
laravellaravel-9

Problem with query on the database with save method


I have a strange problem never seen before Laravel 9. Why will it add data in the UserID column instead of the 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)

Model

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

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

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();

Solution

  • 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