ErrorException Массив для преобразования строки при присоединении - PullRequest
0 голосов
/ 06 мая 2020

У меня есть связь "многие-ко-многим" между двумя таблицами: atentions и linkedintos. это выглядит так: enter image description here

Как видите, в моей сводной таблице есть 2 дополнительных столбца, cantidad и descuento, а также ключи из обеих таблиц. Однако, когда я пытаюсь вставить данные в эту таблицу, я получаю сообщение об ошибке ErrorException Array в преобразование строки , и если я форматирую эти значения с помощью json_encode, я получаю следующее

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '["1","1"]' for column `dentalg`.`atencion_procedimientos`.`cantidad` at row 1 (SQL: insert into `atencion_procedimientos` (`atencion_id`, `cantidad`, `created_at`, `descuento`, `procedimientos_id`, `updated_at`) values (11, ["1","1"], 2020-05-06 02:19:20, ["1","1"], 1, 2020-05-06 02:19:20), (11, ["1","1"], 2020-05-06 02:19:20, ["1","1"], 2, 2020-05-06 02:19:20))

Это функция моего магазина

public function store(Request $request)
    {
        //
        $this->validate($request,[
            'asunto' => 'required',
            'cantidad' => 'required',
            'descuento' => 'required',
            'total_pago' => 'required'
        ]);

        if(Auth::user()->role_id ==1 or Auth::user()->role_id==2){

            $atencion = new Atencion();
            $atencion->doctor_id = Auth::id();
            $atencion->pacientes_id = $request->paciente_id;
            $atencion->asunto = $request->asunto;
            $atencion->tipo_pago = 0; //Tratar de Modificar
            $atencion->estado_pago = 0;
            $atencion->observaciones = $request->observaciones;
            $atencion->total = $request->total_pago;
            $atencion->save();

            $atencion->procedimientos()->attach($request->procedimientos_id,[
                'cantidad' => $request->cantidad,
                'descuento' => $request->cantidad,
            ]);

            Toastr::success('Tratamiento añadido con éxito','success');
            return redirect()->route('admin.Atenciones.index');




        } else {
            Toastr::error('You are not authorized to do this operation','Error');
            return redirect()->back();
        }

Модели

public function procedimientos(){
        return $this->belongsToMany('App\Procedimientos')->withPivot('cantidad', 'descuento')->withTimestamps();
    }

и

public function atenciones(){
        return $this->belongsToMany('App\Atencion')->withPivot('cantidad', 'descuento')->withTimestamps();
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...