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!!!