When I submit the form, it shows me this: error: Integrity constraint violation: 1452 Can not add or update to child row: a foreign key constraint fails (bolsa_empleo.aplicante_empleo, CONSTRAINT aplicantes_empleo_aplicante_usuario_id_foreign FOREIGN KEY (aplicante_usuario_id) REFERENCES applicantes (id) ON DELETE CASCADE) (SQL: insert into applicante_empleo (aplicador_usuario_id, created_at, empleo_id, updated_at) values (4, 2018-10-01 23:46:56, 2, 2018-10-01 23:46:56))
How can I insert in two related tables? In this case, your relationship would be many to many. I do not know if creating mysql relationships in advance could be causing the problem ... if you need more information, please let me know. I am using laravel 5.6 I have tried with attach () but without results and it shows me the same message.
This is the code that is running for the task. First save the data in the table of applicants and then I should make an insert in aplicante_empleo, which is the intermediate table, but this part is not working and shows me the aforementioned error.
public function store(Request $request, $id) {
$this->validate($request, [
'carta_solicitud' => 'required'
]);
$aplicante = new Aplicante;
$aplicante->carta_solicitud = $request->input('carta_solicitud');
$aplicante->empleos_id = $request->input('empleo');
$aplicante->estado = 'pendiente';
$aplicante->usuario_id = auth()->user()->id;
$aplicante->save();
$aplicante->empleos()->sync([$id],false);
return redirect("panelusuario");
}
These are the models for the relationships between tables
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Aplicante extends Model
{
protected $primaryKey = 'usuario_id';
public $incrementing = false;
function empleos() {
return $this->belongsToMany('App\Models\Empleo')->withTimeStamps();
}
function user() {
return $this->belongsTo('App\Models\User','usuario_id');
}
function perfil() {
return $this->hasOne('App\Models\Perfil', 'perfil');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Empleo extends Model
{
function categoria() {
return $this->belongsTo('App\Models\EmpleoCategoria');
}
function user() {
return $this->belongsTo('App\Models\User','usuario_id');
}
function aplicantes() {
return $this->belongsToMany('App\Models\Aplicante', 'aplicante_empleo', 'empleo_id', 'aplicante_usuario_id')->withTimeStamps();
}
}
These are the migrations with their respective foreign keys created.
Schema::create('aplicantes', function (Blueprint $table) {
$table->increments('id');
$table->text('carta_solicitud');
$table->string('estado');
$table->timestamps();
$table->integer('usuario_id')->unsigned();
$table->integer('empleo_id')->unsigned();
$table->foreign('empleo_id')->references('id')
->on('empleos')
->onDelete('cascade');
$table->foreign('usuario_id')->references('id')
->on('users')
->onDelete('cascade');
});
Schema::create('aplicante_empleo', function (Blueprint $table) {
$table->increments('id');
$table->integer('aplicante_usuario_id')->unsigned();
$table->integer('empleo_id')->unsigned();
$table->timestamps();
$table->foreign('aplicante_usuario_id')->references('id')
->on('aplicantes')
->onDelete('cascade');
$table->foreign('empleo_id')->references('id')
->on('empleos')
->onDelete('cascade');
});
Schema::create('empleos', function (Blueprint $table) {
$table->increments('id');
$table->string('titulo');
$table->text('cuerpo');
$table->decimal('salario');
$table->integer('categoria_id')->unsigned();
$table->string('posicion_tipo');
$table->string('duracion_proyecto');
$table->integer('usuario_id')->unsigned();
$table->timestamps();
$table->foreign('usuario_id')->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('categoria_id')->references('id')
->on('empleo_categoria')
->onDelete('cascade');
});
Each of the tables is part of the creation of a small employment pool.