我正在尝试在'Categoria'表和'Noticia'表之间创建多对多关系,然后使用Factory生成数据,但是我遇到了这个错误。我创建了一个数据透视表,但是我无法弄清楚出了什么问题,我对此很陌生....
Noticia模型:
命名空间应用;
使用Illuminate \ Database \ Eloquent \ Model;
Noticia类扩展模型{
public function categorias(){
return $this->belongsTo(Categoria::class);
}
public function autores(){
return $this->belongsTo(Autor::class);
}
}
分类模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Categoria extends Model
{
public function noticia()
{
return $this->belongsToMany(Noticia::class);
}
}
数据透视迁移:
class CreateCategoriaNoticiaTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categoria_noticia', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('noticia_id');
$table->foreign('noticia_id')->references('id')->on('noticias');
$table->unsignedBigInteger('categoria_id');
$table->foreign('categoria_id')->references('id')->on('categorias');
$table->timestamps();
});
}
Noticia迁移
class CreateNoticiasTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('autors', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('nombre');
$table->timestamps();
});
Schema::create('noticias', function (Blueprint $table) {
$table->bigIncrements('id');
$table->date('fecha');
$table->string('titulo');
$table->text('contenido');
$table->string('image');
$table->unsignedBigInteger('autor_id');
$table->foreign('autor_id')->references('id')->on('autors');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('noticias');
Schema::dropIfExists('autors');
Schema::dropIfExists('categorias');
}
}
Categoria迁移:
lass CreateCategoriasTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categorias', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('nombre');
$table->timestamps();
});
}
Noticia工厂:
$factory->define(Noticia::class, function (Faker $faker) {
return [
'autor_id' => factory(\App\Autor::class),
'categoria_id'=> factory(\App\Categoria::class),
'titulo' => $faker->sentence(4),
'image' => $faker->randomElement(['img1.jpg', 'img2.jpg', 'img3.jpg', 'img4.jpg']),
'contenido' => $faker->paragraph,
'fecha'=> $faker->date,
];
});
Categoria功能:
$factory->define(Categoria::class, function (Faker $faker) {
return [
'nombre'=> $faker->name
];
});
和种子诺蒂西亚:公共功能run() {
factory(Noticia::class, 100)->create();
}
}
而且我正在获取此错误:SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'categoria_id'(SQL:插入categorias
(categoria_id
,updated_at
,created_at
)值(Ciencia,2020-06-11 22:22:38,2020-06-11 22:22:38))
提前感谢!
categorias
迁移没有定义名为categoria_id
的字段,而只是id
。
[首先要说的是,'Categoria'和'Noticia'是多对多的,所以双方都应该是belongsToMany
。
因此,对于您的Noticia
型号:
public function categorias(){
return $this->belongsToMany(Categoria::class);
}
错误告诉您在Noticia表中没有'categoria_id',这是正确的。
所以您需要做的是。
从迁移中删除此行。
'categoria_id'=> factory(\App\Categoria::class),
然后
factory(App\Noticia::class, 100)->create()->each(function($n) {
$n->categorias()->save(factory(App\Categoria::class)->make());
});
这应该起作用。