phplaravel

Laravel seed issue, laravel is looking for plural table name


i've just started learning Laravel and I have problem generating seed for my test table.

Console error says: "Base table or view not found: 1146 Table 'laravel.testms' doesn't exists..."

My table is called "testm" - I have no idea why it looks for testms

TestmFactory.php

use Faker\Generator as Faker;

$factory->define(App\Testm::class, function (Faker $faker) {
        return [

        'test' => $faker->paragraph
    ];
});

TestmTableSeeder.php

use Illuminate\Database\Seeder;

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

DatabaseSeeder.php

  public function run()
    {

        $this->call(LinksTableSeeder::class);
        $this->call(TestmTableSeeder::class);
    }

app/Testm.php

class Testm extends Model
{
   // Below line fixed my code :-)
     protected $table = 'testm';
     protected $fillable = [
        'test'

    ];
}

Solution

  • From Laravels documentation:

    By convention, the "snake case", plural name of the class will be used as the table name unless another name is explicitly specified.

    And in order to explicitly define the table name in the model, Testm.php in your case, you would want to add the following code to the class:

    protected $table = 'testm';
    

    Hope this helps!