有没有办法从PHP内部而不是从命令行运行Laravel 5播种机。我正在使用的托管不允许我使用命令行。只是为了确认我想在我的应用程序代码中执行相同的操作:
php artisan db:seed
答案 0 :(得分:15)
您可以使用以下方法:
Artisan::call('db:seed');
要获取上次运行命令的输出,您可以使用:
Artisan::output();
答案 1 :(得分:8)
如果需要,您也可以直接拨打Seeder课程。 如果你手动创建了播种机,请确保你做了一个composer dump-autoload。
从那里开始,代码非常简单:
$seeder = new YourSeederClass();
$seeder->run();
答案 2 :(得分:3)
您可以在运行中向播种机添加参数
例如
$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(),
]);
}