我有这样的功能:Laravel,数组未定义?
public function handle()
{
News::truncate();
$client = new Client();
$crawler = $client->request('GET', 'features');
$crawler->filter('div[id=content]>.homefeature')->each(function ($node, $key) {
$title = $node->filter('.plain')->text();
$datepublished = $node->filter('.dateonline')->text();
$description = $node->filter('.teaser-link')->text();
$link = $node->filter('a')->link();
$link_r = $link->getUri();
if ($image = $node->filter('img')->count() > 0) {
$title = $node->filter('.plain')->text();
$datepublished = $node->filter('.dateonline')->text();
$description = $node->filter('.teaser-link')->text();
$link = $node->filter('a')->link();
$link_r = $link->getUri();
$image = $node->filter('img')->image();
$image_s = $image->getUri();
$image_s = preg_replace("/thumb_/", "", $image_s);
$filename = basename($image_s);
$image_path = ('news-gallery/' . $filename);
Image::make($image_s)->save(public_path('news-gallery/' . $filename));
$id = 1+ $key + 1;
$news = News::where('id', $id)->first();
// if news is null
if (!$news) {
$news = new News();
}
$news->title = $title;
$news->datepublished = $datepublished;
$news->description = $description;
$news->link = $link_r;
$news->image = $image_path;
$news->save();
$this->info('Main article ' .$title. ' done succesfully');
});
$crawler->filter('div[id=content]>.teaser-50')->each(function ($node, $key) {
$title = $node->filter('.plain')->text();
$datepublished = $node->filter('.dateonline')->text();
$description = $node->filter('.teaser-link')->text();
$link = $node->filter('a')->link();
$link_r = $link->getUri();
$title = $node->filter('.plain')->text();
$datepublished = $node->filter('.dateonline')->text();
$description = $node->filter('.teaser-link')->text();
$link = $node->filter('a')->link();
$link_r = $link->getUri();
$image = $node->filter('img')->image();
$image_s = $image->getUri();
$image_s = preg_replace("/thumb_/", "", $image_s);
$filename = basename($image_s);
$image_path = ('news-gallery/' . $filename);
Image::make($image_s)->save(public_path('news-gallery/' . $filename));
//$id = 1+ $key + 1;
$newsArray[] = [
'title' => $title,
'datepublished' => $datepublished,
'description' => $description,
'link' => $link_r,
'image' => $image_path,
];
$this->info('Scraping done for ' .$title. ' succesfully');
});
print_r($newsArray);
//$news = News::insert($newsArray);
}
}
所以我试图把一切都放到一个数组保存前,但我得到一个错误说$ newsArray是不确定的?如果我把print_r放到函数中,就在它下面,我会得到一个输出。这怎么解决?我想要做的是循环throught每个结果象现在这样,将其插入到一个数组,后来,批量插入它DB
//例:
public function handle()
{
/* Clear table, set array of id's, and get today's date */
Film::truncate();
$arrayOfCinemaIds = [10565,7311,9434];
$startdate = strtotime("today");
$date = date("Y-m-d", $startdate);
/* Set date to today + + days */
$endate = strtotime("+6 Days");
/* Check if current date is less than $endate */
while ($startdate < $endate) {
/* If it is less, do a foreach loop for $arrayOfCinemaIds */
foreach($arrayOfCinemaIds as $id){
/* Get data from cinelist api */
$data= file_get_contents('https://api.cinelist.co.uk/get/times/cinema/'.$id.'/?day='.$date);
$data = json_decode($data);
/* Find entities that match api_key */
$find_id = Entity::where('api_key', $id)->get();
foreach($find_id as $id_found) {
$entity_id = $id_found->id;
foreach($data->listings as $listing) {
/* Put each record into an array */
$title = $listing->title;
$time = implode(', ', $listing->times);
$films[] = [
'title' => $title,
'times' => $time,
'entity_id' => $entity_id,
'date' => $date,
];
}
}
}
/* Increase $startdate by 1 day, display message, and change $date to new date */
$startdate = strtotime("+1 Days", $startdate);
$this->info('Scraping for ' .$date. ' done, moving to next one');
$date = date("Y-m-d", $startdate);
}
/* Insert array of films to DB, display message */
$film = Film::insert($films);
$this->info('Succesfully Added Films to DB!');
}
我有这等工匠命令,而且这个工作非常好,那么为什么不是上面那个?
声明数组$ newsArray = array(); – JYoThI
声明你的'$ newsArray = array();'函数外部,并在函数 –