Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel seed issue, laravel is looking for plural table name

Tags:

laravel

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'

    ];
}
like image 548
riten Avatar asked Mar 18 '18 19:03

riten


2 Answers

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!

like image 150
Derek Pollard Avatar answered Sep 22 '22 01:09

Derek Pollard


Try adding this to your model

protected $table = 'testm';
like image 32
Jigs1212 Avatar answered Sep 19 '22 01:09

Jigs1212