Soy nueva con esto de Laravel. A ver si alguien puede ayudarme. Tengo tres tablas. Una es de tareas para gestionar, la otra es de los responsables de realizarlas y la otra es de ciudades donde se realizan. Además tengo una tabla pivote que debe relacionar las tres tablas, mediante los id. La tabla pivote, además, debe llevar un indicador de si la tarea está o no activa (un campo con un `0` o un `1`, que se llama `activa`). Las migraciones son las siguientes. La de la tabla de tareas (`tasks`) es así: <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateTasksTable extends Migration { public function up() { Schema::create('tasks', function (Blueprint $table) { $table->increments('id'); $table->string('tarea', 100); $table->text('descripcion')->nullable(); $table->date('f_inicio'); $table->date('f_final'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('tasks'); } } La de la tabla de responsables (`managers`) es así: <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateManagersTable extends Migration { public function up() { Schema::create('managers', function (Blueprint $table) { $table->increments('id'); $table->string('nombre', 60); $table->string('cargo', 60); $table->timestamps(); }); } public function down() { Schema::dropIfExists('managers'); } } La de la tabla de ciudades (`cities`) es así: <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateCitiesTable extends Migration { public function up() { Schema::create('cities', function (Blueprint $table) { $table->increments('id'); $table->string('ciudad', 60); $table->string('pais', 60); $table->timestamps(); }); } public function down() { Schema::dropIfExists('cities'); } } La de la tabla pivote (`city_manager_task`) es así: <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateCitiesManagersTasksTable extends Migration { public function up() { Schema::create('city_manager_task', function (Blueprint $table) { $table->increments('id'); $table->integer('city_id')->unsigned(); $table->foreign('city_id')->references('id')->on('cities'); $table->integer('manager_id')->unsigned(); $table->foreign('manager_id')->references('id')->on('managers'); $table->integer('task_id')->unsigned(); $table->foreign('task_id')->references('id')->on('tasks'); $table->char('activa', 1); $table->timestamps(); }); } public function down() { Schema::dropIfExists('city_manager_task'); } } Los modelos los tengo definidos así. El modelo de ciudades (`City`) es así: <?php namespace App; use App\Manager; use App\Task; use Illuminate\Database\Eloquent\Model; class City extends Model { protected $table = 'cities'; protected $fillable = [ 'ciudad', 'pais', ]; public function managers() { return $this->belongsToMany(Manager::class, 'city_manager_task')->withTimestamps()->withPivot('activa'); } public function tasks() { return $this->belongsToMany(Task::class, 'city_manager_task')->withTimestamps()->withPivot('activa'); } } El modelo de responsables (`Manager`) es así: <?php namespace App; use App\City; use App\Task; use Illuminate\Database\Eloquent\Model; class Manager extends Model { protected $table = 'managers'; protected $fillable = [ 'nombre', 'cargo', ]; public function cities() { return $this->belongsToMany(City::class, 'city_manager_task')->withTimestamps()->withPivot('activa'); } public function tasks() { return $this->belongsToMany(Task::class, 'city_manager_task')->withTimestamps()->withPivot('activa'); } } El modelo de tareas (`Task`) es así: <?php namespace App; use App\City; use App\Manager; use Illuminate\Database\Eloquent\Model; class Task extends Model { protected $table = 'tasks'; protected $fillable = [ 'tarea', 'descripcion', 'f_inicio', 'f_final', ]; public function cities() { return $this->belongsToMany(City::class, 'city_manager_task')->withTimestamps()->withPivot('activa'); } public function managers() { return $this->belongsToMany(Manager::class, 'city_manager_task')->withTimestamps()->withPivot('activa'); } } El último modelo es el de la tabla pivote (`CityManagerTask`) es asi: <?php namespace App; use App\City; use App\Manager; use App\Task; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Pivot; class CityManagerTask extends Pivot { protected $table = 'city_manager_task'; protected $fillable = [ 'city_id', 'manager_id', 'task_id', 'activa', ]; } Las factories también las tengo. La de ciudades (`CityFactory`) es así: <?php use Faker\Generator as Faker; $factory->define(App\City::class, function (Faker $faker) { return [ 'ciudad' => $faker->city, 'pais' => $faker->country ]; }); La de responsables (`ManagerFactory`) es así: <?php use Faker\Generator as Faker; $factory->define(App\Manager::class, function (Faker $faker) { return [ 'nombre' => $faker->name(), 'cargo' => $faker->randomElement([ 'Team leader', 'Controller', 'Advisor', 'Manager' ]), ]; }); La de tareas (`TaskFactory`) es así: <?php use Faker\Generator as Faker; $factory->define(App\Task::class, function (Faker $faker) { return [ 'tarea' => $faker->randomElement([ 'Gestión de marketing', 'Control de divisas', 'Mobiliario urbano', 'Medición de ruido', 'Creación de equipos', 'Estructuración de paisajes' ]), 'descripcion' => $faker->sentence(20), 'f_inicio' => $faker->dateTimeInInterval('- 2 years', '+ 6 months'), 'f_final' => $faker->dateTimeInInterval('- 1 years', '+ 6 months'), ]; }); Por último, el seeder (DatabaseSeeder) para que se ejecuten las factories es el siguiente: <?php use App\City; use App\Manager; use App\Task; use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { public function run() { $cities = factory(City::class, 20)->create(); $managers = factory(Manager::class, 10)->create(); factory(Task::class, 100)->create() ->each(function ($task) use ($cities, $managers) { $task->cities() ->attach($cities ->random(mt_rand(5, 15)) ->pluck('id') )->managers() ->attach($managers) ->pluck('id'); } ); } } Las migraciones se están ejecutando bien, y se crean perfectamente las estructuras, con sus claves foráneas, sin problemas. El problema viene cuando trato de ejecutar el seeder, para que ejecute las factories. Se supone que debe llenar las tres tablas de datos (eso sí lo hace bien), pero, además, debe cumplimentar la tabla pivote. Al ejecutarlo, me responde: Illuminate\Database\QueryException : SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: city_manager_task.manager_id (SQL: insert into "city_manager_task" ("city_id", "created_at", "task_id", "updated_at") select 2 as "city_id", 2019-01-11 12:47:54 as "created_at", 1 as "task_id", 2019-01-11 12:47:54 as "updated_at" union all select 3 as "city_id", 2019-01-11 12:47:54 as "created_at", 1 as "task_id", 2019-01-11 12:47:54 as "updated_at" union all select 7 as "city_id", 2019-01-11 12:47:54 as "created_at", 1 as "task_id", 2019-01-11 12:47:54 as "updated_at" union all select 8 as "city_id", 2019-01-11 12:47:54 as "created_at", 1 as "task_id", 2019-01-11 12:47:54 as "updated_at" union all select 9 as "city_id", 2019-01-11 12:47:54 as "created_at", 1 as "task_id", 2019-01-11 12:47:54 as "updated_at" union all select 10 as "city_id", 2019-01-11 12:47:54 as "created_at", 1 as "task_id", 2019-01-11 12:47:54 as "updated_at" union all select 12 as "city_id", 2019-01-11 12:47:54 as "created_at", 1 as "task_id", 2019-01-11 12:47:54 as "updated_at" union all select 19 as "city_id", 2019-01-11 12:47:54 as "created_at", 1 as "task_id", 2019-01-11 12:47:54 as "updated_at") at C:\xampp\htdocs\trestablas\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664 660| // If an exception occurs when attempting to run a query, we'll format the error 661| // message to include the bindings with SQL, which will make this exception a 662| // lot more helpful to the developer instead of just the database's errors. 663| catch (Exception $e) { > 664| throw new QueryException( 665| $query, $this->prepareBindings($bindings), $e 666| ); 667| } 668| Exception trace: 1 PDOException::("SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: city_manager_task.manager_id") C:\xampp\htdocs\trestablas\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458 2 PDOStatement::execute() C:\xampp\htdocs\trestablas\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458 Y, no tengo ni idea de qué estoy haciendo mal. Lo que sea, está en el seeder, creo. Además, necesito que al cumplimentar la tabla pivote me introduzca, en cada registro, en el campo `activa`, el valor `0` o `1`. Es decir. Cada registro de la tabla pivote debería tener el id de una tarea, el de un responsable, el de una ciudad, y el valor que indique si la tarea está activa o no. Si alguien que sepa de Laravel puede ayudarme, o decirme donde documentarme (la documentación de Laravel no habla de este uso de las relaciones) os lo agradeceré.