Good I am trying to fill two tables of related data bases in Laravel using ajax
one of books
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateLibrosTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('libros', function(Blueprint $table)
{
$table->increments('id');
$table->string('titulo');
$table->integer('id_categoria')->unsigned();
$table->integer('id_autor')->unsigned();
$table->integer('id_editorial')->unsigned();
$table->integer('cantidad');
$table->date('fecha_lanzamiento');
$table->string('idioma');
$table->integer('paginas');
$table->string('descripcion');
$table->string('tipo');
$table->string('enlace');
$table->timestamps();
});
}
and one of categories
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriasTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categorias', function(Blueprint $table)
{
$table->increments('id');
$table->string('nombre');
$table->integer('libro_id')->unsigned();
$table->foreign('libro_id')->references('id')->on('libros');
$table->timestamps();
});
}
and the AJAX scrip
$("#registro").click(function()
{
var dato = $("#categoria").val();
var route = "http://localhost:8000/categoria";
var token = $("#token").val();
$.ajax({
url: route,
headers:{'X-CSRF-TOKEN': token},
type: "POST",
dataType:'json',
data: {categoria: dato},
success:function(){
$("#msj-success").fadeIn();
},
error:function(msj){
$("#msj").html(msj.responseJSON.categoria);
$("#msj-error").fadeIn();
}
});
});
at the time of trying to fill the tabala categories I get the following error:
QueryException in Connection.php line 624: SQLSTATE [23000]: Integrity constraint violation: 1452 Can not add or update to child row: a foreign key constraint fails ( biblioteca
. categorias
, CONSTRAINT categorias_libro_id_foreign
FOREIGN KEY ( libro_id
) REFERENCES libros
( id
)) (SQL: insert into categorias
( updated_at
, created_at
) values (2017-01-05 19:06:28, 2017-01 -05 19:06:28))
If someone can identify what I am doing wrong, I would appreciate it if you mentioned it