在PHP中将Eloquent Model作为闭包参数传递



我在Laravel应用程序之外使用Laravel照明/数据库。我试图通过雄辩模型作为我的闭包参数,但它抛出一个错误。也许我传错了。我的代码如下:

      // 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);
});

相关内容

  • 没有找到相关文章

最新更新