/** * */ public function run() { $posts = $this->post->all(); $names = ['Gigi', 'Ion', 'Boss', 'Hater']; foreach ($posts as $post) { foreach ($this->comments as $comment) { $comment = $this->comment->create(['name' => $names[array_rand($names)], 'content' => $comment, 'post_id' => $post['id']]); $replies = mt_rand(0, count($this->comments)); for ($i = 0; $i < $replies; $i++) { $this->comment->create(['name' => $names[array_rand($names)], 'content' => $this->comments[$i], 'parent_id' => $comment['id'], 'post_id' => $comment['post_id']]); } } } }
/** * Get comments by post id * * @param $postId * @return mixed */ public function findByPostId($postId) { $data = ['post_id' => $postId]; return $this->comment->search($data); }