You see, I have the following tables:
Schema::create('empresas', function (Blueprint $table){
$table->increments('id');
$table->string('nombre');
$table->timestamps();
});
Schema::create('coches', function (Blueprint $table){
$table->increments('id');
$table->string('nombre');
$table->unsignedInteger('empresa_id');
$table->foreign('empresa_id')->references('id')->on('empresas');
$table->integer('precio');
$table->timestamps();
});
So, I prepare the following in DatabaseSeeder.php:
public function run(){
$a=DB::table('empresas')->insert([
'nombre'=>'Ford'
]);
$b=DB::table('empresas')->insert([
'nombre'=>'PSA'
]);
$c=DB::table('empresas')->insert([
'nombre'=>'Volkswagen'
]);
DB::table('coches')->insert([
'nombre'=>'Alfalfa Romeo',
'empresa_id'=>$a,
'precio'=>54000,
]);
DB::table('coches')->insert([
'nombre'=>'Bionic Diesel',
'empresa_id'=>$a,
'precio'=>27000
]);
DB::table('coches')->insert([
'nombre'=>'Sitsume Kaso',
'empresa_id'=>$b,
'precio'=>45000
]);
DB::table('coches')->insert([
'nombre'=>'Audi Fono',
'empresa_id'=>$b,
'precio'=>59600
]);
DB::table('coches')->insert([
'nombre'=>'Renault Polo',
'empresa_id'=>$c,
'precio'=>50900
]);
DB::table('coches')->insert([
'nombre'=>'Canis Lupus',
'empresa_id'=>$c,
'precio'=>49900
]);
}
This should create 3 companies and 6 cars, assigning every 2 of these a company, but I find the following:
They all have the id of the first company! How do I solve this?