FlyingWalrus
2 years ago

artisan db:seed gives "Unable to locate factory with name [default] ..."

Posted 2 years ago by FlyingWalrus

When running artisan db:seed I somehow keep getting the error

[InvalidArgumentException]                                
  Unable to locate factory with name [default] [App\Text].

Looks like I missed something. Do I have any errors in my setup?

my database/seeds/DatabaseSeeder.php

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UsersTableSeeder::class);
        $this->call(TextTableSeeder::class);
    }
}

my database/seeds/TextTableSeeder

<?php

use Illuminate\Database\Seeder;

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

my /app/Text.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Text extends Model
{
    //
}

the /database/migrations/whatever_create_texts_table.php

<?php

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

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

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

and the /database/factories/ModelFactory.php

<?php


/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/

/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(App\User::class, function (Faker\Generator $faker) {
    static $password;

    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'password' => $password ?: $password = bcrypt('secret'),
        'remember_token' => str_random(10),
    ];

});

$factory->define(App\Text::class, function (Faker\Generator $faker) {

    return [
        'text' => $faker->sentence,
    ];
});

So I think everything necessary should be there, or?

Please sign in or create an account to participate in this conversation.