/** * @param $postData * @return mixed */ public static function normalize(&$postData) { $postId = sprintf('%d_%d', $postData['from_id'], $postData['id']); $post = PostModel::firstOrNew(['external_id' => $postId]); self::$__count_imports_posts++; if (!$post->id) { self::$__count_imports_new_posts++; } $text = ''; if ($postData['post_type'] == 'copy') { $text = isset($postData['copy_text']) && !empty($postData['copy_text']) ? $postData['copy_text'] : ''; } $text .= !empty($text) && !empty($postData['text']) ? '<br>' : ''; $text .= !empty($postData['text']) ? $postData['text'] : ''; $post->date = Carbon::createFromTimestamp($postData['date']); $post->text = $text; $post->save(); // dump($text, $post->text); if (isset($postData['attachments'])) { $attachments = self::attachmentsPost($postData['attachments']); if (sizeof($attachments) > 0) { $post->attachments()->sync($attachments, false); } } return $post; }
/** * Execute the console command. * * @return mixed */ public function handle() { $yaml = new Parser(); $posts = $yaml->parse(file_get_contents(base_path($this->argument('file')))); foreach ($posts['posts'] as $post) { $slug = Str::slug($post['title']); $newPost = Post::firstOrNew(['slug' => $slug]); $newPost->timestamps = false; $newPost->title = $post['title']; $newPost->subtitle = $post['subtitle']; $newPost->body = $post['body']; $newPost->frontpage = $post['frontpage']; if (isset($post['coverImage'])) { $newPost->coverImage = $post['coverImage']; } $date = Carbon::createFromTimestamp($post['created_at']); $newPost->created_at = $date; $newPost->updated_at = Carbon::now(); $newPost->save(); $newPost->events()->detach(); if (isset($post['events']) && is_array($post['events'])) { $events = (new Event())->whereIn('id', $post['events'])->get(); if (!$events->isEmpty()) { $newPost->events()->saveMany($events); } } $this->info("Loaded: {$newPost->title}"); } }
/** * @param array $postRaw * @return \App\Post */ protected function __normalizePosts(&$postRaw) { $postId = sprintf('%d_%d', $postRaw['from_id'], $postRaw['id']); $post = Post::firstOrNew(['external_id' => $postId]); $post->date = Carbon::createFromTimestamp($postRaw['date']); $post->text = $postRaw['text']; $post->save(); $attachments = $this->__attachmentsPost($postRaw['attachments']); if (sizeof($attachments) > 0) { $post->attachments()->sync($attachments, false); } return $post; }
private function updatePosts($data) { foreach ($data as $row) { $post = Post::firstOrNew(['id' => $row->id]); $post->authour_id = $row->authour_id; $post->slug = $row->slug; $post->title = $row->title; $post->subtitle = $row->subtitle; $post->content_raw = $row->content_raw; $post->content_html = $row->content_html; $post->page_image = $row->page_image; $post->meta_description = $row->meta_description; $post->is_draft = $row->is_draft; $post->layout = $row->layout; // $post->published_at = $row->published_at; $post->thumb_url = $row->thumb_url; $post->save(); } return '/admin/post'; }
public function testCreate() { // test firstOrCreate $post = Post::firstOrCreate(['content' => 'post 1']); // record exists $this->assertEquals(1, $post->id); $post = Post::firstOrCreate(['content' => 'post xxx']); // record does not exist $post = Post::where('content', '=', 'post xxx')->first(); $this->assertEquals('post xxx', $post->content); // test firstOrNew $post = Post::firstOrNew(['content' => 'post 1']); // record exists $this->assertEquals(1, $post->id); $post = Post::firstOrNew(['content' => 'post yyy']); // record does not exist $this->assertEquals('post yyy', $post->content); $this->assertFalse(property_exists($post, 'user_id')); $post = Post::where('content', '=', 'post yyy')->first(); $this->assertNull($post); }