在 PHP 中将 Eloquent 模型作为闭包参数传递
Pass Eloquent Model As An Closure Argument In PHP
我在 laravel 应用程序之外使用 Laravel Illuminate/Database。我试图将 Eloquent 模型作为我的闭包参数传递,但它抛出了一个错误。可能是我错误地传递了它。我的代码如下:
// Create a dummy subject (This is working absolutely fine)
SubjectModel::create(array(
'title' => 'Mathematics',
'description' => 'Math Subject',
'slug' => 'math',
'ka_url' => 'http://khanacademy.org/math'
));
$scrapper = new SubjectScrapper();
$scrapper->setUrl('');
这是行不通的。 SubjectModel 未在以下闭包中传递
$scrapper->runScrapper(function($subjects) use ($scrapper, SubjectModel $subjectModel) {
if(!empty($subjects))
{
foreach ($subjects as $subject) {
$urlParts = explode('/', $subject['url']);
$slug = end($urlParts);
$subjectModel::create(array(
'title' => $subject['subject_name'],
'slug' => $slug,
'ka_url' => $scrapper->getBaseUrl().$subject['link'],
));
}
}
});
谁能告诉我如何完成这个任务。
试试这个。不需要在闭包中传递对象
$scrapper = new SubjectScrapper();
$scrapper->setUrl('');
$scrapper->runScrapper(function($subjects) use ($scrapper, $output) {
SubjectModel::create(array(
'title' => 'Math',
'slug' => 'math',
'ka_url' => 'http://math'
));
$output->writeln('<info>Total Subjects Scrapped:: '.count($subjects).'</info>'.PHP_EOL);
});
我在 laravel 应用程序之外使用 Laravel Illuminate/Database。我试图将 Eloquent 模型作为我的闭包参数传递,但它抛出了一个错误。可能是我错误地传递了它。我的代码如下:
// Create a dummy subject (This is working absolutely fine)
SubjectModel::create(array(
'title' => 'Mathematics',
'description' => 'Math Subject',
'slug' => 'math',
'ka_url' => 'http://khanacademy.org/math'
));
$scrapper = new SubjectScrapper();
$scrapper->setUrl('');
这是行不通的。 SubjectModel 未在以下闭包中传递
$scrapper->runScrapper(function($subjects) use ($scrapper, SubjectModel $subjectModel) {
if(!empty($subjects))
{
foreach ($subjects as $subject) {
$urlParts = explode('/', $subject['url']);
$slug = end($urlParts);
$subjectModel::create(array(
'title' => $subject['subject_name'],
'slug' => $slug,
'ka_url' => $scrapper->getBaseUrl().$subject['link'],
));
}
}
});
谁能告诉我如何完成这个任务。
试试这个。不需要在闭包中传递对象
$scrapper = new SubjectScrapper();
$scrapper->setUrl('');
$scrapper->runScrapper(function($subjects) use ($scrapper, $output) {
SubjectModel::create(array(
'title' => 'Math',
'slug' => 'math',
'ka_url' => 'http://math'
));
$output->writeln('<info>Total Subjects Scrapped:: '.count($subjects).'</info>'.PHP_EOL);
});