是否有一种方法可以在 PHP 中运行 Laravel 5 数据填充器而不是从命令行运行。我使用的托管服务不允许我使用命令行。只是想确认我想要在我的应用程序代码中执行相当于这个的操作:
php artisan db:seed
如果需要,您还可以直接调用Seeder类。只需确保在手动创建填充程序时进行了composer dump-autoload。
从那里开始,代码非常简单:
$seeder = new YourSeederClass();
$seeder->run();
您可以使用以下方法:
Artisan::call('db:seed');
要获取上一次运行命令的输出,您可以使用:
Artisan::output();
php artisan db:seed --class=User
的内容?我看到您将数组传递给 Artisan::call()
函数,但是当我使用 Artisan::call('db:seed', ['class'=>'Users'])
时,我会收到无效参数异常,指出“'class'”参数不存在。 - KirklandArtisan::call('db:seed',['--class' => 'YourSeederClass']);
。 - Sven $newSchool = School::create($data);
$schoolMeals = new \MealSeeder();
$schoolMeals->run($newSchool->id);
//学校餐饮
public function run($school = 1)
{
$time = Carbon::now();
App\Meal::create([
'school_id' => $school,
'name' => 'Breakfast',
'slug' => 'breakfast',
'description' => 'Test Meal',
'start_time' => $time->toTimeString(),
'end_time' => $time->addMinutes(60)->toTimeString(),
]);
App\Meal::create([
'school_id' => $school,
'name' => 'Lunch',
'slug' => 'lunch',
'description' => 'Test Meal',
'start_time' => $time->toTimeString(),
'end_time' => $time->addMinutes(60)->toTimeString(),
]);
App\Meal::create([
'school_id' => $school,
'name' => 'Supper',
'slug' => 'supper',
'description' => 'Test Meal',
'start_time' => $time->toTimeString(),
'end_time' => $time->addMinutes(60)->toTimeString(),
]);
}
$this->call(MySeeder::class)
。 - alvaropgl