【发布时间】:2018-02-26 06:47:43
【问题描述】:
我是 laravel 的新手。 当我想通过这个命令运行种子时:php artisan db:seed,我得到这个错误:
[Symfony\Component\Debug\Exception\FatalThrowableError] 调用 未定义的函数表()
我的两个播种机类:
1- GroupTableSeeder
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class GroupTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::statement('SET FOREIGN_KEY_CHECKS=0');
DB::table('groups')->truncate();
$groups = [
['id' => 1, 'name' => 'Family', 'created_at' => new DateTime, 'updated_at' => new DateTime ],
['id' => 2, 'name' => 'Friends', 'created_at' => new DateTime, 'updated_at' => new DateTime ],
['id' => 3, 'name' => 'Customers', 'created_at' => new DateTime, 'updated_at' => new DateTime ],
['id' => 4, 'name' => 'CoWorkers', 'created_at' => new DateTime, 'updated_at' => new DateTime ],
['id' => 5, 'name' => 'Teachers', 'created_at' => new DateTime, 'updated_at' => new DateTime ]
];
DB:table('groups')->insert($groups);
}
}
2- ContactsTableSeeder
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use Faker\Factory as Faker;
class ContactsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
DB::table( 'contacts' )->truncate();
$faker = Faker::create();
$contacts = [];
foreach ( range( 1, 20 ) as $index ) {
$contacts[] = [
'name' => $faker->name,
'email' => $faker->email,
'phone' => $faker->phoneNumber,
'address' => "{$faker->streetName} {$faker->postcode} {$faker->city}",
'company' => $faker->company,
'created_at' => new DateTime,
'updated_at' => new DateTime,
];
}
DB::table( 'contacts' )->insert( $contacts );
}
}
还有我的 DatabaseSeeder 类:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call(GroupTableSeeder::class);
$this->call(ContactsTableSeeder::class);
}
}
请帮我解决。
谢谢。
【问题讨论】:
-
DB 类的
use语句在哪里? -
我添加了使用 Illuminate\Support\Facades\DB;到这 3 个文件。但仍然无法正常工作并出现相同的错误
-
你能显示整个 Laravel 错误吗?错误中可能包含更多详细信息,可以帮助您解决该问题。
-
播种:GroupTableSeeder [Symfony\Component\Debug\Exception\FatalThrowableError] 调用未定义的函数 table() 这就是 cmd 中显示的所有错误