Thomas Le Naour
Thomas Le Naour

Reputation: 49

Problem with Laravel Seeding with Factories

I have a problem with Laravel Seeding with Factories :

I have three tables users, products and products_categories. I have a ProductFactory class which is called in ProductSeeder. Finally, I have my DatabaseSeeder who executes all seeders.

When I run php artisan db:seed, I get an error with the table name. The seeder try to insert values into product_categories while my table name is products_categories. I don't know how can I fix this error.

Seeding: ProductSeeder

   Illuminate\Database\QueryException  : SQLSTATE[42S02]: Base table or view not found: 1146 Table 'b2-laravel-uf.product_categories' doesn't exist (SQL: insert into `product_categories` (`name`, `updated_at`, `created_at`) values (Mathématiques, 2019-11-22 13:32:52, 2019-11-22 13:32:52))

ProductFactory.php

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Product;
use Faker\Generator as Faker;

$factory->define(Product::class, function (Faker $faker) {
    return [
        'reference' => $faker->randomNumber(6),
        'name' => $faker->sentence(5),
        'description' => $faker->text(200),
        'price_ht' => 9.99,
        'category_id' => factory(App\ProductCategory::class),
        'owner_id' => factory(App\User::class)
    ];
});

ProductSeeder.php

<?php

use Illuminate\Database\Seeder;

class ProductSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {   
        factory(App\Product::class, 30)->create();
    }
}

DatabaseSeeder.php

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(ProductSeeder::class);
    }
}

Migration for Products Categories table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProductsCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products_categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products_categories');
    }
}

Thank you in advance.

Upvotes: 0

Views: 1218

Answers (2)

BadPiggie
BadPiggie

Reputation: 6359

You need to change the generated table name to products_categories,

In your ProductCategory Model,

protected $table = 'product_categories';

Upvotes: 1

Alberto
Alberto

Reputation: 12909

Probably is caused by Laravel and how it 'generate' the table name if it's not explicit declared, so just declare the table name as follow in the ProductCategory model:

protected $table = 'product_categories'

Upvotes: 2

Related Questions