protected function migrate_news() { $website = Website::first(); $data_users = User::get(); foreach ($data_users as $user) { $users[$user['username']] = $user; } Content::truncate(); // get old articles DB::connection('mongodb')->collection('posts')->where('category', 'regex', '/news/i')->chunk(1000, function ($data) use($new_collection, $users, $website) { foreach ($data as $x) { unset($tags); $tags[] = 'news'; if (str_is('*arema*', strtolower($x['title'])) || str_is('*isl*', strtolower($x['title'])) || str_is('*ipl*', strtolower($x['title'])) || str_is('*cronus*', strtolower($x['title']))) { $tags[] = 'arema'; } if (str_is('*persema*', strtolower($x['title']))) { $tags[] = 'persema'; } if (str_is('*[lipsus]*', strtolower($x['title']))) { $tags[] = 'lipsus'; } if (str_is('*[press release]*', strtolower($x['title']))) { $tags[] = 'press release'; } if (str_is('*gempa*', strtolower($x['title']))) { $tags[] = 'press release'; } if (str_is('*pilkada*', strtolower($x['title']))) { $tags[] = 'pilkada'; } if (str_is('*halo malang*', strtolower($x['title'])) || str_is('*halomalang*', strtolower($x['title']))) { $tags[] = 'halomalang'; } if (str_is('*wisata*', strtolower($x['title'])) || str_is('*halomalang*', strtolower($x['title']))) { $tags[] = 'wisata'; } if (str_is('*beasiswa*', strtolower($x['title'])) || str_is('*guru*', strtolower($x['title'])) || str_is('*pendidikan*', strtolower($x['title']))) { $tags[] = 'pendidikan'; } if (str_is('*bpjs*', strtolower($x['title']))) { $tags[] = 'bpjs'; } if ($x['tgl_published']) { $news = new Content(['title' => $x['title'], 'slug' => str_replace('.', '', $x['url']), 'summary' => str_limit(strip_tags(str_replace("\n", "", $x['full'])), 125), 'content' => $x['full'], 'published_at' => date('Y-m-d H:i:s', $x['tgl_published']->sec), 'created_at' => $x['tgl'], 'updated_at' => $x['tgl'], 'user_id' => array_key_exists($x['author'], $users) ? $users[$x['author']]->id : $users['dita']->id]); if (!$news->save()) { print_r($news->toArray()); dd($news->getErrors()); } // ---------------------------------------------------------------------------------------------------- // IMAGE // ---------------------------------------------------------------------------------------------------- $s_image = new Image(['name' => 'sm', 'path' => $x['thumbmail'], 'title' => '', 'description' => '']); $m_image = new Image(['name' => 'md', 'path' => $x['image'], 'title' => '', 'description' => '']); $l_image = new Image(['name' => 'lg', 'path' => $x['image'], 'title' => '', 'description' => '']); $news->images()->updateOrCreate(['name' => 'sm'], $s_image->toArray()); $news->images()->updateOrCreate(['name' => 'md'], $m_image->toArray()); $news->images()->updateOrCreate(['name' => 'lg'], $l_image->toArray()); // ---------------------------------------------------------------------------------------------------- // TAG // ---------------------------------------------------------------------------------------------------- unset($tags_model); $tags_model = new Collection(); foreach ($tags as $tag) { $tags_model[] = Tag::firstOrCreate(['name' => $tag]); } foreach ($tags_model as $k => $v) { if (!$tags_model[$k]->save()) { dd($tags_model[$k]->getErrors()); } } if (count($tags_model)) { $news->tags()->sync($tags_model->lists('id')); } // ---------------------------------------------------------------------------------------------------- // WEBSITE // ---------------------------------------------------------------------------------------------------- $news->websites()->sync([$website->id]); // ---------------------------------------------------------------------------------------------------- // LOG // ---------------------------------------------------------------------------------------------------- // $news->authored()->attach(array_key_exists($x['author'], $users) ? $users[$x['author']]->id : $users['dita']->id, ['original_data' => json_encode([]), 'updated_data' => $news->toJson()]); } // $news->websites()->associate($website); // $news->save(); } }); // save to new tables }