Reputation: 17
Model:
protected $table = 'citas_odontologicas';
protected $fillable = ['fecha','hora', 'procedimiento_id', 'paciente_id'];
Migration:
public function up()
{
Schema::create('citas_odontologicas', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->string('fecha');
$table->string('hora');
$table->bigInteger('procedimiento_id')->unsigned();
$table->foreign('procedimiento_id')->references('id')->on('procedimientos');
$table->bigInteger('paciente_id')->unsigned();
$table->foreign('paciente_id')->references('id')->on('pacientes');
});
}
Controller:
public function agendarCita($fecha, $hora, $procedimiento, $paciente)
{
$citaOdontologica = new CitaOdontologica($fecha, $hora, $procedimiento, $paciente);
$citaOdontologica->save();
dd($citaOdontologica);
}
Error:
Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1364 Field 'fecha' doesn't have a default value (SQL: insert into
citas_odontologicas
(updated_at
,created_at
) values (2020-01-25 20:01:22, 2020-01-25 20:01:22))
Upvotes: 0
Views: 763
Reputation: 638
Laravel Model provides static API for creating objects. If you want to use new ClassName()
notation, you have to override your model constructor.
Remember about _id
suffix for your relation fields (if you did not change it).
But i suggest to use Model::create()
and Model::make()
methods, like this:
$citaOdontologica = CitaOdontologica::make([
'fecha' => $fecha,
'hora' => $hora,
'procedimiento_id' => $procedimiento,
'paciente_id' => $paciente
]);
$citaOdontologica->save();
or even simplier:
$citaOdontologica = CitaOdontologica::create([
'fecha' => $fecha,
'hora' => $hora,
'procedimiento_id' => $procedimiento,
'paciente_id' => $paciente
]);
Upvotes: 1