0
votes

Im trying to create a many to many relation between 'Categoria' table and 'Noticia' table and then with Factory generate data, but i'm getting that error. I have created a pivot table, but i cant figure out whats wrong, i'm pretty new to this....

The Noticia MODEL:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Noticia extends Model {

public function categorias(){ 
    return $this->belongsTo(Categoria::class); 
}
public function autores(){ 
    return $this->belongsTo(Autor::class); 
}

}

The Categoria Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Categoria extends Model
{
    public function noticia()
    {
        return $this->belongsToMany(Noticia::class);
    }

}

The Pivot Migration:

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

The Noticia Migration

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');

    }
}

The Categoria Migration:

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

The Noticia Factory:

$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,
    ];
});

The Categoria Factopry:

$factory->define(Categoria::class, function (Faker $faker) {
    return [
        'nombre'=> $faker->name

    ];
});

And the Seeds Noticia: public function run() {

    factory(Noticia::class, 100)->create();

}

}

And i'M gettiing this error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'categoria_id' in 'field list' (SQL: insert into categorias (categoria_id, updated_at, created_at) values (Ciencia, 2020-06-11 22:22:38, 2020-06-11 22:22:38))

Thanks in advance!!!

2
are you still having problem?Andy Song
Yes, can't find the way...Bejotta

2 Answers

1
votes

The categorias migration doesn't define a field named categoria_id, it's just id.

1
votes

A few things, first as you said, 'Categoria' and 'Noticia' are many-To-many, so both sides should be belongsToMany.

So for your Noticia model:

public function categorias(){ 
    return $this->belongsToMany(Categoria::class); 
}

And the error is telling you that you do not have the 'categoria_id' in your Noticia table which is true.

So what you need to do is that.

Remove this line from the migration.

'categoria_id'=> factory(\App\Categoria::class),

And then

factory(App\Noticia::class, 100)->create()->each(function($n) {
  $n->categorias()->save(factory(App\Categoria::class)->make());
});

This should work.