2016-09-11 51 views
0

我在Laravel应用程序之外使用Laravel Illuminate/Database。我试图通过Eloquent模型作为我的闭包参数,但它抛出一个错误。可能是我错误地传递了它。我的代码如下:在PHP中传递雄辩模型作为闭包参数

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

有谁请告诉我如何完成这个任务。

回答

1

试试这个。无需在关闭时传递对象

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