Php Laravel 4数据库种子不';行不通
我将遵循以下教程: 本教程: 但是,当我尝试运行php artisan db:seed时,什么都没有发生 我试试这个:Php Laravel 4数据库种子不';行不通,php,laravel,eloquent,laravel-4,Php,Laravel,Eloquent,Laravel 4,我将遵循以下教程: 本教程: 但是,当我尝试运行php artisan db:seed时,什么都没有发生 我试试这个: <?php // app/database/seeds/groups.php return array( 'table' => 'groups', array( 'name' => 'Administrador', 'description' => '<
<?php
// app/database/seeds/groups.php
return array(
'table' => 'groups',
array(
'name' => 'Administrador',
'description' => '<p>Permissão total no sistema</p>',
'created_at' => new DateTime,
'updated_at' => new DateTime
),
array(
'name' => 'Moderadores',
'description' => '<p>Podem apenas postar e moderar comentários</p>',
'created_at' => new DateTime,
'updated_at' => new DateTime
)
);
但是:
本教程中的示例是错误的-因为种子在Beta 1和Beta 2之间的工作方式发生了变化 将您的
DatabaseSeeder.php
文件更改为以下格式-它将适用于本教程:
<?php
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call('UserTableSeeder');
}
}
class UserTableSeeder extends Seeder {
public function run()
{
DB::table('users')->delete();
User::create(array(
'id' => 1,
'username' => 'firstuser',
'password' => Hash::make('first_password'),
'created_at' => new DateTime,
'updated_at' => new DateTime
));
User::create(array(
'id' => 2,
'username' => 'seconduser',
'password' => Hash::make('second_password'),
'created_at' => new DateTime,
'updated_at' => new DateTime
));
}
}
如果@Shift Exchange返回一些错误,如“未找到类用户”,您可以尝试
DB::table('users')->insert(array(...))
// DatabaseSeeder.php
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
...
$this->call('NoticesTableSeeder');
$this->command->info('Notices table seeded!');
...
}
}
而不是
User::create(array(...))
创建您的播种机:
<?php
// NoticesTableSeeder.php
class NoticesTableSeeder extends Seeder {
public function run()
{
DB::table('notices')->truncate();
$notices = [
['title' => 'Our Web Page Is Back Online!', 'body' => 'Our web site is online again. Thanks for visiting.', 'created_at' => new DateTime],
['title' => 'Sample New Notice 1', 'body' => 'Sample new notice content.', 'created_at' => new DateTime],
['title' => 'Sample New Notice 2', 'body' => 'Sample new notice content.', 'created_at' => new DateTime],
['title' => 'Sample New Notice 3', 'body' => 'Sample new notice content.', 'created_at' => new DateTime],
['title' => 'Sample New Notice 4', 'body' => 'Sample new notice content.', 'created_at' => new DateTime]
];
DB::table('notices')->insert($notices);
}
}
// DatabaseSeeder.php
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
...
$this->call('NoticesTableSeeder');
$this->command->info('Notices table seeded!');
...
}
}
composer dump-autoload
php artisan db:seed
更换自动加载器:
<?php
// NoticesTableSeeder.php
class NoticesTableSeeder extends Seeder {
public function run()
{
DB::table('notices')->truncate();
$notices = [
['title' => 'Our Web Page Is Back Online!', 'body' => 'Our web site is online again. Thanks for visiting.', 'created_at' => new DateTime],
['title' => 'Sample New Notice 1', 'body' => 'Sample new notice content.', 'created_at' => new DateTime],
['title' => 'Sample New Notice 2', 'body' => 'Sample new notice content.', 'created_at' => new DateTime],
['title' => 'Sample New Notice 3', 'body' => 'Sample new notice content.', 'created_at' => new DateTime],
['title' => 'Sample New Notice 4', 'body' => 'Sample new notice content.', 'created_at' => new DateTime]
];
DB::table('notices')->insert($notices);
}
}
// DatabaseSeeder.php
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
...
$this->call('NoticesTableSeeder');
$this->command->info('Notices table seeded!');
...
}
}
composer dump-autoload
php artisan db:seed
为数据库设置种子:
<?php
// NoticesTableSeeder.php
class NoticesTableSeeder extends Seeder {
public function run()
{
DB::table('notices')->truncate();
$notices = [
['title' => 'Our Web Page Is Back Online!', 'body' => 'Our web site is online again. Thanks for visiting.', 'created_at' => new DateTime],
['title' => 'Sample New Notice 1', 'body' => 'Sample new notice content.', 'created_at' => new DateTime],
['title' => 'Sample New Notice 2', 'body' => 'Sample new notice content.', 'created_at' => new DateTime],
['title' => 'Sample New Notice 3', 'body' => 'Sample new notice content.', 'created_at' => new DateTime],
['title' => 'Sample New Notice 4', 'body' => 'Sample new notice content.', 'created_at' => new DateTime]
];
DB::table('notices')->insert($notices);
}
}
// DatabaseSeeder.php
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
...
$this->call('NoticesTableSeeder');
$this->command->info('Notices table seeded!');
...
}
}
composer dump-autoload
php artisan db:seed
太好了,你完了我将测试并给出您的反馈,请稍等。行DB::table('users')->delete()代码>是否不受欢迎有没有一种方法可以像迁移文件一样在databaseseeder文件之外填充它?我已经测试了几种方法,但没有一种有效。它只有在我填充databaseseeder.php文件中的数据时才起作用
// DatabaseSeeder.php
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
...
$this->call('NoticesTableSeeder');
$this->command->info('Notices table seeded!');
...
}
}