Ejemplo n.º 1
0
 protected function migrate_peta()
 {
     $website = Website::first();
     $data_users = User::get();
     foreach ($data_users as $user) {
         $users[$user['username']] = $user;
     }
     // get old articles
     DB::connection('mongodb')->collection('posts')->where('category', 'regex', '/directory/i')->chunk(1000, function ($data) use($new_collection, $users, $website) {
         foreach ($data as $x) {
             if (!$x['full']) {
                 continue;
             }
             unset($tags);
             $tags[] = 'directory';
             foreach ($x['category'] as $v) {
                 unset($tmp_tag);
                 foreach (explode('|', $v) as $tmp) {
                     if (!str_is('directory', $tmp)) {
                         $tmp_tag .= $tmp . ' ';
                     }
                 }
                 if ($tmp_tag) {
                     $tags[] = $tmp_tag;
                 }
             }
             if ($x['tgl_published']) {
                 $news = new Directory(['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' => trim($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]);
                 // ----------------------------------------------------------------------------------------------------
                 // ADDRESS
                 // ----------------------------------------------------------------------------------------------------
                 $news->addresses()->save(new Address(['road' => $x['extra_field']['directory_alamat'], 'city' => str_is("*batu*", strtolower($x['extra_field']['directory_alamat'])) ? "Batu" : "Malang", 'longitude' => $x['extra_field']['directory_map'][0][0] && $x['extra_field']['directory_map'][0][1] ? $x['extra_field']['directory_map'][0][0] : null, 'latitude' => $x['extra_field']['directory_map'][0][0] && $x['extra_field']['directory_map'][0][1] ? $x['extra_field']['directory_map'][0][1] : null]));
             }
             // $news->websites()->associate($website);
             // $news->save();
         }
     });
     // save to new tables
 }