Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
El campo Laravel 5.4 no tiene un valor predeterminado

Tengo este error y ninguno de los resultados buscados en Google que verifiqué es similar a mi problema.

Tengo una aplicación con clase Trato, Usuario y Coincidencias

Un acuerdo tiene muchas coincidencias. Un usuario tiene muchas coincidencias. Un usuario tiene muchas ofertas.

Estoy intentando crear una nueva Partida usando mi objeto Trato

 $deal->matches()->create(['user_id'=>$id]);

Esta es mi clase de coincidencia, he definido todas las relaciones necesarias

 class Match extends Model { /** * The attributes that are mass assignable. * * @var array */ protected $guarded = []; public $timestamps = false; public $expired_on = ""; public static function boot() { parent::boot(); static::creating(function ($model) { $model->matched_on = $model->freshTimestamp(); }); } public function __construct(){ $d = (new \DateTime($this->matched_on))->modify('+1 day'); $this->expired_on = $d->format('Ymd H:i:s'); } /** * Get the user that owns the match. */ public function user() { return $this->belongsTo('App\User'); } /** * Get the deal that owns the match. */ public function deal() { return $this->belongsTo('App\Deal'); } }

Y sigo recibiendo este error cuando intento crear una nueva coincidencia.

QueryException en Connection.php línea 647: SQLSTATE[HY000]: Error general: 1364 El campo 'user_id' no tiene un valor predeterminado (SQL: insertar en valores de matches ( deal_id ) (1))

Tengo mi guardado para ser una matriz vacía, ¿cuál podría ser el problema?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Elimine la matriz guarded y agregue la fillable en su lugar:

 protected $fillable = ['user_id', 'deal_id'];
about 4 years ago · Santiago Trujillo Report

0

Si desea volver al comportamiento anterior, actualice su

config/base de datos.php

archivo y establezca 'strict' => false para su conexión.

about 4 years ago · Santiago Trujillo Report

0

Dado que era un campo único en mi caso, no pude hacerlo anulable.

Para mí, tenía un constructor vacío que estaba causando el problema, no sé por qué. Por favor comente si alguien sabe la razón.

 public function __construct(){ }

Comentar/eliminarlo resolvió el problema.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!