Illuminate\Database\QueryException: SQLSTATE: General error: 1364
P粉011684326
P粉011684326 2024-04-03 22:39:00
0
1
369

Why can't I enter data?

In the appointment table you don't want to enter the userid attribute, it has a good correlation, someone help me

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1364 Field 'idusuario' has no default value (Connection: mysql, SQL: INSERT citas(idpaciente, idtrabajador, fecha_hora, observaciones, precio_tratamiento, precio_total, estadopago,updated_at, created_at) value (1, 1, 2023-08-01 17:26:08, ?, 0, 0, 0, 2023-08-30 15:19:42, 2023-08-30 15:19:42)) In the file C:\larragon\www\apipodo\vendor\laravel\framework\src\Illuminate\Database\Connection.php line 795

public function store(Request $request)
    {
        // --------------------------------------------------------------------
        //  "fecha_hora": "2023-08-01 17:26:08", // Reqerido
        //  "idtrabajador": 1, // Requerido
        //  "idusuario": 1, // Requerido
        //  "idpaciente" : 1 // Requerido
        //  "precio_tratamiento" : 0, // Nullable
        //  "observaciones" : "", // Nullable
        //  "precio_total" : 0, // Nullable
        //  "estadopago" : 0, // Nullable
        //  "diagnostico" : '', // Nullable
        //  "tratamiento" : '', // Nullable

        // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


        // ----------------------------------------------------------------
        // ----------------------------------------------------------------
        // ----------------------------------------------------------------
        $validator = \Validator::make($request->input(), $this->getRulesInput(), $this->getMessagesErrors());
        if ($validator->fails()) {
            return response()->json([
                'status' => false,
                'message' => 'Error en la validación',
                'errors' => $validator->errors()->all()
            ], 400);
        }
        // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


        // ----------------------------------------------------------------
        // ----------------------------------------------------------------
        // ----------------------------------------------------------------
        // Insertar el id del usuario usando el token
        $idusuario = 1;
        // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


        // ----------------------------------------------------------------
        // ----------------------------------------------------------------
        // ----------------------------------------------------------------
        // Insertar datos en la tabla cita
        $dataCita = [
            'fecha_hora' => $request->input('fecha_hora'),
            'precio_tratamiento' => $request->input('precio_tratamiento'),
            'observaciones' => $request->input('observaciones'),
            'precio_total' => $request->input('precio_total'),
            'estadopago' => $request->input('estadopago'),
            'idtrabajador' => $request->input('idtrabajador'),
            'idpaciente' => $request->input('idpaciente'),
            'idusuario' => 1,
        ];
        $cita = Cita::create($request->all($dataCita));
        // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


        // ----------------------------------------------------------------
        // ----------------------------------------------------------------
        // ----------------------------------------------------------------
        // Insertar datos en la tabla detalle cita 
        // --- Condicion: si no esta vacio el campo diagnostico o tratamiento
        // --- Validar si el diagnostico y el tratamiento ya existen, 
        // --- si es que no existen hay que crearlos
        $dataDetalleCita = [];
        if (!$request->input('diagnostico')) {
            $diagnostico = Diagnostico::where('diagnostico', '=', $request->input('diagnostico'));
            if (!$diagnostico) $diagnostico = Diagnostico::create(['diagnostico' => $request->input('diagnostico')]);
            $dataDetalleCita['iddiagnostico'] = $diagnostico['iddiagnostico'];
        }
        if (!$request->input('tratamiento')) {
            $tratamiento = Tratamiento::where('tratamiento', '=', $request->input('tratamiento'));
            if (!$tratamiento) $tratamiento = Tratamiento::create(['tratamiento' => $request->input('tratamiento')]);
            $dataDetalleCita['idtratamiento'] = $tratamiento['idtratamiento'];
        }
        if ($dataDetalleCita) {
            $dataDetalleCita['idcita'] = $cita['idcita'];
            Detalle_cita::create($dataDetalleCita);
        }
        // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

        return response()->json([
            'status' => true,
            'message' => 'Cita creada exitosamente',
            'data' => $cita
        ], 201);
    }
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Cita extends Model
{
    use HasFactory;

    protected $table = 'citas';

    protected $primaryKey = 'idcita';

    protected $fillable = [
        'fecha_hora', 'precio_tratamiento', 'observaciones',
        'precio_total', 'estadopago', 'idtrabajador', 'idpaciente', 'idusuario',
    ];

    public function trabajador()
    {
        return $this->belongsTo(Trabajador::class, 'idtrabajador');
    }

    public function paciente()
    {
        return $this->belongsTo(Paciente::class, 'idpaciente', 'idPaciente');
    }

    public function usuario()
    {
        return $this->belongsTo(User::class, 'idusuario', 'id');
    }

    public function detalleCita()
    {
        return $this->hasMany(Detalle_cita::class, 'idcita');
    }
}
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('citas', function (Blueprint $table) {
            $table->bigIncrements('idcita');
            $table->timestamp('fecha_hora');
            $table->decimal('precio_tratamiento', 10, 2)->default(0);
            $table->string('observaciones', 255)->nullable();
            $table->decimal('precio_total', 10, 2)->default(0);
            $table->tinyInteger('estadopago')->nullable()->default(0);
            $table->unsignedBigInteger('idtrabajador');
            $table->unsignedBigInteger('idpaciente');
            $table->unsignedBigInteger('idusuario');
            $table->timestamps();

            $table->index('idtrabajador');
            $table->index('idpaciente');
            $table->index('idusuario');

            $table->foreign('idtrabajador')
                ->references('idtrabajador')
                ->on('trabajador')
                ->onDelete('NO ACTION')
                ->onUpdate('NO ACTION');

            $table->foreign('idpaciente')
                ->references('idPaciente')
                ->on('pacientes')
                ->onDelete('NO ACTION')
                ->onUpdate('NO ACTION');

            $table->foreign('idusuario')
                ->references('id')
                ->on('users')
                ->onDelete('NO ACTION')
                ->onUpdate('NO ACTION');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('citas');
    }
};

I don't understand why it doesn't work

P粉011684326
P粉011684326

reply all(1)
P粉478445671

I think the problem is in the create line.

Instead of $cita = Cita::create($request->all($dataCita)); Just write $cita = Cita::create($dataCita);

Because you have prepared $dataCita using the parameters in $request.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!