Php Laravel error SQLSTATE[42S22]:未找到列:1054未知列';分类id';在';字段列表';
我试图在“Categoria”表和“Noticia”表之间创建一个多对多关系,然后使用工厂生成数据,但我得到了那个错误。我已经创建了一个透视表,但我不知道出了什么问题,我对这个很陌生 Noticia模型: 名称空间应用程序 使用Illumb\Database\Elount\Model 类Noticia扩展模型 { } 分类模型:Php Laravel error SQLSTATE[42S22]:未找到列:1054未知列';分类id';在';字段列表';,php,laravel,factory,Php,Laravel,Factory,我试图在“Categoria”表和“Noticia”表之间创建一个多对多关系,然后使用工厂生成数据,但我得到了那个错误。我已经创建了一个透视表,但我不知道出了什么问题,我对这个很陌生 Noticia模型: 名称空间应用程序 使用Illumb\Database\Elount\Model 类Noticia扩展模型 { } 分类模型: <?php namespace App; use Illuminate\Database\Eloquent\Model; class Categoria
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Categoria extends Model
{
public function noticia()
{
return $this->belongsToMany(Noticia::class);
}
}
诺西西亚移民
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');
}
}
分类迁移:
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();
});
}
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,
];
});
分类因素包括:
$factory->define(Categoria::class, function (Faker $faker) {
return [
'nombre'=> $faker->name
];
});
种子呢
通知:
公共功能运行()
{
}
我发现了这个错误:SQLSTATE[42S22]:未找到列:“字段列表”中的1054未知列“categoria\u id”(SQL:insert-intocategorias
(categoria\u id
,updated\u at
,created\u at
)值(Ciencia,2020-06-11 22:22:382020-06-11 22:38))
提前感谢 categorias迁移没有定义一个名为
categoria\u id
的字段,它只是idcategorias
迁移没有定义一个名为categoria\u id
的字段,它只是id几件事,首先正如你所说,“categoria”和“Noticia”是多对多的,因此,双方都应属于同一方
因此,对于您的Noticia
型号:
公共函数categorias(){
返回$this->belongtomany(Categoria::class);
}
错误是告诉您Notifia表中没有“categoria_id”,这是真的
所以你需要做的就是这样
从迁移中删除此行
'categoria\u id'=>工厂(\App\categoria::class),
然后
factory(App\Noticia::class,100)->create()->每个函数($n){
$n->categorias()->save(工厂(App\Categoria::class)->make());
});
这应该行得通。有几件事,首先,正如你所说,“分类”和“通知”是多对多的,所以双方都应该属于同一类
因此,对于您的Noticia
型号:
公共函数categorias(){
返回$this->belongtomany(Categoria::class);
}
错误是告诉您Notifia表中没有“categoria_id”,这是真的
所以你需要做的就是这样
从迁移中删除此行
'categoria\u id'=>工厂(\App\categoria::class),
然后
factory(App\Noticia::class,100)->create()->每个函数($n){
$n->categorias()->save(工厂(App\Categoria::class)->make());
});
这应该行。我想不出如何解决它,我仍然有问题…我想不出如何解决它,我仍然有问题…你仍然有问题吗?是的,找不到路…你仍然有问题吗?是的,找不到路。。。
$factory->define(Categoria::class, function (Faker $faker) {
return [
'nombre'=> $faker->name
];
});
factory(Noticia::class, 100)->create();
}