From 3e2bf7a1cc0ca3ce7954720bd9f702d299d43054 Mon Sep 17 00:00:00 2001 From: Eric Ouyang Date: Fri, 7 Nov 2014 14:54:43 -0500 Subject: [PATCH 1/7] update completed example --- .../2014_11_07_155735_create_posts_table.php | 2 +- app/models/Comment.php | 1 + app/models/Post.php | 14 +++++++++++ app/routes.php | 16 ++++++------- app/views/blog/partials/post.blade.php | 5 ++-- app/views/blog/view.blade.php | 24 ++++++++++++------- 6 files changed, 42 insertions(+), 20 deletions(-) diff --git a/app/database/migrations/2014_11_07_155735_create_posts_table.php b/app/database/migrations/2014_11_07_155735_create_posts_table.php index 36dd8d9..df38eb8 100644 --- a/app/database/migrations/2014_11_07_155735_create_posts_table.php +++ b/app/database/migrations/2014_11_07_155735_create_posts_table.php @@ -28,7 +28,7 @@ public function up() */ public function down() { - Schema::drop('users'); + Schema::drop('posts'); } } diff --git a/app/models/Comment.php b/app/models/Comment.php index 434ae92..94e4be7 100644 --- a/app/models/Comment.php +++ b/app/models/Comment.php @@ -2,6 +2,7 @@ class Comment extends Eloquent { + // many-to-one relationship with the Post model public function post() { return $this->belongsTo('Post'); diff --git a/app/models/Post.php b/app/models/Post.php index 26aa602..e2b0d4f 100644 --- a/app/models/Post.php +++ b/app/models/Post.php @@ -2,14 +2,28 @@ class Post extends Eloquent { + // one-to-many relationship with the Comment model public function comments() { return $this->hasMany('Comment'); } + // helper function to get the URL of a post public function getURL() { return URL::action('viewPost', array('id' => $this->id)); } + // helper function to get a string of the number of comments + public function getNumCommentsStr() + { + $num = $this->comments()->count(); + + if ($num == 1) + { + return '1 comment'; + } + + return $num . ' comments'; + } } diff --git a/app/routes.php b/app/routes.php index 7a9c042..ca72451 100644 --- a/app/routes.php +++ b/app/routes.php @@ -15,21 +15,21 @@ // routes for the blog Route::get('/post/new', array( - 'as' => 'newPost', - 'uses' => 'BlogController@newPost' + 'as' => 'newPost', + 'uses' => 'BlogController@newPost' )); Route::post('/post/new', array( - 'as' => 'createPost', - 'uses' => 'BlogController@createPost' + 'as' => 'createPost', + 'uses' => 'BlogController@createPost' )); Route::get('/post/{id}', array( - 'as' => 'viewPost', - 'uses' => 'BlogController@viewPost' + 'as' => 'viewPost', + 'uses' => 'BlogController@viewPost' )); Route::post('/post/{id}/comment', array( - 'as' => 'createComment', - 'uses' => 'BlogController@createComment' + 'as' => 'createComment', + 'uses' => 'BlogController@createComment' )); diff --git a/app/views/blog/partials/post.blade.php b/app/views/blog/partials/post.blade.php index 814a2d3..125a457 100644 --- a/app/views/blog/partials/post.blade.php +++ b/app/views/blog/partials/post.blade.php @@ -13,7 +13,8 @@ {{ str_limit($post->content, 430) }} @endif -