[英]Pass Eloquent Model As An Closure Argument In PHP
I'm using Laravel Illuminate/Database outside of laravel application. 我在laravel应用程序之外使用Laravel Illuminate / Database。 I'm trying to pass the Eloquent model as my closure argument but its throwing an error.
我正在尝试将Eloquent模型作为我的闭包参数,但是它引发了错误。 May be I'm passing it wrongly.
可能是我错误地传递了它。 My code is as following:
我的代码如下:
// 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('');
This is not working. 这是行不通的。 SubjectModel is not being passed in the following closure
在以下闭包中未传递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'],
));
}
}
});
Could anybody please tell me how to accomplish this task. 有人可以告诉我如何完成这项任务吗?
Try this. 尝试这个。 No need to pass object in closure
无需在闭包中传递对象
$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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.