Laravel错误SQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列“ categoria_id”

时间:2020-06-11 22:38:06

标签: php laravel factory

我试图在“ Categoria”表和“ Noticia”表之间创建多对多关系,然后使用Factory生成数据,但是我遇到了这个错误。我已经创建了一个数据透视表,但是我不知道出了什么问题,我对此很陌生...。

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();
        });
    }

诺蒂西亚移民

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();
        });
    }

诺蒂西亚工厂:

$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:插入categoriascategoria_id,{{ 1}},updated_at)值(Ciencia,2020-06-11 22:22:38,2020-06-11 22:22:38))

提前谢谢!

2 个答案:

答案 0 :(得分:1)

categorias迁移未定义名为categoria_id的字段,而只是id

答案 1 :(得分:1)

首先要说的是,“类别”和“ 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());
});

这应该有效。

相关问题