Having all my classes added, I press the send button in which the json is sent and it makes a submit to a form (it is seen in the ajax that is below) which is filled before entering the modal that is seen below in the picture.
I am trying to send the data of an array to my database, I get the array by decoding a json that I receive through ajax, which I create from a DataTable fnGetData().
I correctly receive the json in the controller and when decoding it, I am left with a main array in which I have many arrays, each one of which is a class (each class is a row of the datatable)
To make everything clearer, this is my table
In this way I send the json to my controller
$.ajax({
url:"{{ route('alumno.horario') }}",
type: 'POST',
dataType:'json',
contentType: 'json',
data: JSON.stringify(p),
contentType: 'application/json; charset=utf-8',
success : function(){
$('#alumno-frm').submit(); // ENVIO FORMULARIO DE ALUMNO MEDIANTE SU RUTA
$("#tablaAgregados").DataTable().clear().draw();
},
error: function(){
//-----
}
});
This way I receive and decode the json in my controller
public function storeHorario(Request $request){ // GUARDAR HORARIO
$data = json_decode($request->getContent());
dump($data);
// return json_encode($data);
}
and this is the result of the decoded json
array:2 [
0 => array:10 [
0 => 1
1 => "20/11/2019"
2 => "18:13"
3 => "120 minutos"
4 => "NOMBRE PROFESOR"
5 => "Toyota Yaris 2A-1S-D2"
6 => "<button type="button" class="btn btn-outline-danger elevation-1 btn_eliminar">Quitar</button>"
7 => "47"
8 => "6"
]
1 => array:10 [
0 => 2
1 => "20/11/2019"
2 => "18:13"
3 => "120 minutos"
4 => "NOMBRE PROFESOR"
5 => "Toyota Yaris 2A-1S-D2"
6 => "<button type="button" class="btn btn-outline-danger elevation-1 btn_eliminar">Quitar</button>"
7 => "47"
8 => "6"
]
]
try to send a single class to my timetable in this way, but it doesn't work for me (ajax returns error)
public function storeHorario(Request $request){ // GUARDAR HORARIO
$data = json_decode($request->getContent());
$array_data = $data['0'];
$dato2 = $array_data['0'];
$dato3 = $array_data['1'];
$dato4 = $array_data['2'];
$dato5 = 'practica';
$dato6 = $array_data['3'];
$dato7 = $array_data['7'];
$dato8 = $array_data['8'];
$horario = horarioModel::updateOrCreate(
[
'clase' => $dato2,
'fecha' => $dato3,
'hora' => $dato4,
'tipo' => $dato5,
'minutos_bloque' => $dato6,
'id_profesor' => $dato7,
'id_vehiculo' => $dato8
]
);
return json_encode($data);
}
What I need is to capture the data 0,1,2,3,7,8 and 9 from each array and send them as a class from my schedule to their respective fields in the table.
time table model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class horarioModel extends Model
{
protected $table = 'horario';
protected $primaryKey = 'id_horario';
protected $fillable = [
'id_horario',
'clase',
'fecha',
'hora',
'tipo',
'minutos_bloque',
'id_curso',
'id_profesor',
'id_alumno',
'id_vehiculo',
'id_estado'
];
}
The simplest thing would be to iterate over the array with a foreach. Following the logic with which you are already working, something like this occurs to me:
Just keep in mind that if there is a lot of data, making queries within a foreach is intense for the DB, so you should find another way other than with updateOrCreate.
Invalid datetime format: 1366 Incorrect integer value: ''
youDB
wait for a formatFecha
and you are passing itString
the format must be
Y-m-d