Laravel 错误 SQLSTATE[42S22]:找不到列:1054 'field list' 中的未知列 'categoria_id'
Laravel error SQLSTATE[42S22]: Column not found: 1054 Unknown column 'categoria_id' in 'field list'
我试图在 'Categoria' table 和 'Noticia' table 之间创建多对多关系,然后使用 Factory 生成数据,但我遇到了这个错误.我创建了一个枢轴 table,但我不知道出了什么问题,我对此很陌生....
Noticia 型号:
命名空间应用;
使用Illuminate\Database\Eloquent\Model;
class 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');
}
}
类别迁移:
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
];
});
还有种子
注意事项:
public 函数 运行()
{
factory(Noticia::class, 100)->create();
}
}
我正在解决这个错误:SQLSTATE[42S22]:未找到列:1054 'field list' 中的未知列 '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 table 中没有 '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());
});
这应该有效。
我试图在 'Categoria' table 和 'Noticia' table 之间创建多对多关系,然后使用 Factory 生成数据,但我遇到了这个错误.我创建了一个枢轴 table,但我不知道出了什么问题,我对此很陌生....
Noticia 型号:
命名空间应用;
使用Illuminate\Database\Eloquent\Model;
class 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');
}
}
类别迁移:
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
];
});
还有种子 注意事项: public 函数 运行() {
factory(Noticia::class, 100)->create();
}
}
我正在解决这个错误:SQLSTATE[42S22]:未找到列:1054 'field list' 中的未知列 '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 table 中没有 '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());
});
这应该有效。