/** * a version specific to Post that handles author, tags and categories * @return array */ public function toArray() { $arr = ['title' => $this->title, 'slug' => $this->slug, 'date' => $this->date, 'body' => $this->body, 'author' => $this->author->toArray(), 'tags' => [], 'categories' => []]; foreach ($this->tags as $tag) { $arr['tags'][] = $tag->toArray(); } foreach ($this->categories as $category) { $arr['categories'][] = $category->toArray(); } return $arr; }
protected function postExtendedTableFormatToModels($tableResults) { $posts = []; foreach ($tableResults as $row) { $props = $row['post']; $props['categories'] = []; foreach ($row['categories'] as $categoryArray) { $props['categories'][] = Category::fromArray($categoryArray); } $props['tags'] = []; foreach ($row['tags'] as $tagArray) { $props['tags'][] = Tag::fromArray($tagArray); } $props['author'] = Author::fromArray($row['author']); $posts[] = Post::fromArray($props); } return $posts; }