我刚刚开始学习Laravel,我在为测试表生成种子时遇到了问题。
控制台错误说: "未找到基本表格或视图:1146表' laravel.testms'不存在......"
我的桌子名为" testm" - 我不知道为什么它会寻找testm s
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);
}
应用程序/ Testm.php
class Testm extends Model
{
// Below line fixed my code :-)
protected $table = 'testm';
protected $fillable = [
'test'
];
}
答案 0 :(得分:4)
按照惯例," snake case",该类的复数名称将用作表名,除非明确指定了另一个名称。
为了在模型中明确定义表名,Testm.php
,您需要将以下代码添加到类中:
protected $table = 'testm';
希望这有帮助!
答案 1 :(得分:1)
尝试将此添加到您的模型中
protected $table = 'testm';