mysql - Best possible schema of post, comments and replies -


i trying figure out schema of post, comments , replies comments, replies single level(no reply reply).
post:

1) id 2) user_id 3) contents 4) privacy 

comment:

1) id 2) user_id 3) post_id 4) content 

reply:

1) id 2) user_id 3) comment_id 4) content 

what possible way minimize queries , best results.

according above current database structure following relationships between tables.

  • posts & comments = onetomany
  • comments & replies = onetomany

you can read more relationships in laravel

hence need provide following code in respective model.

class post extends eloquent {     /**      * comments assign single post      * @return \illuminate\database\eloquent\collection      */     public function comments()     {         return $this->hasmany('app\comment','post_id');     } }  class comment extends eloquent {     /**      * replies assign single comment      * @return \illuminate\database\eloquent\collection      */     public function replies()     {         return $this->hasmany('app\reply','comment_id ');     } } 

now have got posts db:

$posts = post::all(); if($posts->count() > 0) {     foreach($posts $post) {         // single post information         $comments = $post->comments;         if($comments->count() > 0) {             foreach($comments $comment) {                 // single comment information                   $replies = $comment->replies;                  if($replies->count() > 0) {                     foreach($replies $reply) {                         // single reply information                      }                  }             }         }     } } 

if have fetch single post:

$post = post::find($postid); if($post->count() > 0) {     $comments = $post->comments;     if($comments->count() > 0) {         foreach($comments $comment) {             // single comment information               $replies = $comment->replies;              if($replies->count() > 0) {                 foreach($replies $reply) {                     // single reply information                  }              }         }     } } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -