From 41f601a01898b23aab8d7865777dc96670f2f307 Mon Sep 17 00:00:00 2001 From: Subhadip Ghorui Date: Wed, 7 Oct 2020 14:13:35 +0530 Subject: [PATCH] Comment system done --- app/Comment.php | 17 ++ .../Controllers/Admin/CommentController.php | 24 +++ app/Http/Controllers/CommentController.php | 25 +++ .../Controllers/User/CommentController.php | 30 ++++ app/Post.php | 4 + app/User.php | 4 + ...020_10_07_072827_create_comments_table.php | 34 ++++ .../views/admin/comments/index.blade.php | 166 ++++++++++++++++++ .../layouts/backend/partials/navbar.blade.php | 23 +++ resources/views/post.blade.php | 100 ++++------- resources/views/user/comments/index.blade.php | 166 ++++++++++++++++++ resources/views/user/index.blade.php | 160 +++++++++++++++-- routes/web.php | 7 +- 13 files changed, 678 insertions(+), 82 deletions(-) create mode 100644 app/Comment.php create mode 100644 app/Http/Controllers/Admin/CommentController.php create mode 100644 app/Http/Controllers/CommentController.php create mode 100644 app/Http/Controllers/User/CommentController.php create mode 100644 database/migrations/2020_10_07_072827_create_comments_table.php create mode 100644 resources/views/admin/comments/index.blade.php create mode 100644 resources/views/user/comments/index.blade.php diff --git a/app/Comment.php b/app/Comment.php new file mode 100644 index 0000000..5cad142 --- /dev/null +++ b/app/Comment.php @@ -0,0 +1,17 @@ +belongsTo('App\Post'); + } + public function user() + { + return $this->belongsTo('App\User'); + } +} diff --git a/app/Http/Controllers/Admin/CommentController.php b/app/Http/Controllers/Admin/CommentController.php new file mode 100644 index 0000000..fc96463 --- /dev/null +++ b/app/Http/Controllers/Admin/CommentController.php @@ -0,0 +1,24 @@ +delete(); + Toastr::success('Comment successfully deleted :)'); + return redirect()->back(); + } +} diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php new file mode 100644 index 0000000..f050f11 --- /dev/null +++ b/app/Http/Controllers/CommentController.php @@ -0,0 +1,25 @@ +validate($request, ['comment' => 'required|max:1000']); + $comment = new Comment(); + $comment->post_id = $post; + $comment->user_id = Auth::id(); + $comment->comment = $request->comment; + $comment->save(); + + // Success message + Toastr::success('success', 'The comment created successfully ;)'); + return redirect()->back(); + } +} diff --git a/app/Http/Controllers/User/CommentController.php b/app/Http/Controllers/User/CommentController.php new file mode 100644 index 0000000..44cb087 --- /dev/null +++ b/app/Http/Controllers/User/CommentController.php @@ -0,0 +1,30 @@ +latest()->get(); + return view('user.comments.index', compact('comments')); + } + public function destroy($id) + { + $comment = Comment::findOrFail($id); + if ($comment->user_id == Auth::id()) { + $comment->delete(); + Toastr::success('Comment successfully deleted :)'); + return redirect()->back(); + } else { + Toastr::error('You can not delete this comment :('); + return redirect()->back(); + } + } +} diff --git a/app/Post.php b/app/Post.php index 0a93183..ef7b7b2 100644 --- a/app/Post.php +++ b/app/Post.php @@ -19,6 +19,10 @@ public function tags() { return $this->hasMany('App\Tag', 'postID', 'id'); } + public function comments() + { + return $this->hasMany('App\Comment'); + } // Define Scope // published() diff --git a/app/User.php b/app/User.php index ad6cf8d..5227fcf 100644 --- a/app/User.php +++ b/app/User.php @@ -47,4 +47,8 @@ public function posts() { return $this->hasMany('App\Post'); } + public function comments() + { + return $this->hasMany('App\Comment'); + } } diff --git a/database/migrations/2020_10_07_072827_create_comments_table.php b/database/migrations/2020_10_07_072827_create_comments_table.php new file mode 100644 index 0000000..b78da5b --- /dev/null +++ b/database/migrations/2020_10_07_072827_create_comments_table.php @@ -0,0 +1,34 @@ +id(); + $table->unsignedBigInteger('post_id'); + $table->unsignedBigInteger('user_id'); + $table->text('comment'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('comments'); + } +} diff --git a/resources/views/admin/comments/index.blade.php b/resources/views/admin/comments/index.blade.php new file mode 100644 index 0000000..ff977cb --- /dev/null +++ b/resources/views/admin/comments/index.blade.php @@ -0,0 +1,166 @@ +@extends('layouts.backend.app') +@push('header') + + + +@endpush +@section('content') +
+ + +
+
+
+
+ @if ($errors->any()) + + @foreach ($errors->all() as $error) + + @endforeach + + @endif + +
+
+
+
+ Comments Table +
+
+ + + + + + + + + + + + + @foreach ($comments as $key => $comment) + + + + + + + + + @endforeach + + +
#CommentUserPostCreated_AtAction
{{$key+1}}{{$comment->comment}}{{$comment->user->name}}{{$comment->post->title}}{{$comment->created_at->diffForHumans()}} + +
+ +
+
+
+
+
+ +
+ @foreach ($comments as $comment) + + @endforeach + + + +
+ + + +@endsection + +@push('footer') + + + + + + + + + + + + + + + {!! Toastr::message() !!} +@endpush diff --git a/resources/views/layouts/backend/partials/navbar.blade.php b/resources/views/layouts/backend/partials/navbar.blade.php index 1c2f8a7..582d433 100644 --- a/resources/views/layouts/backend/partials/navbar.blade.php +++ b/resources/views/layouts/backend/partials/navbar.blade.php @@ -21,6 +21,7 @@ class="navbar-toggler"
diff --git a/resources/views/post.blade.php b/resources/views/post.blade.php index 4f4d18f..a2c9886 100644 --- a/resources/views/post.blade.php +++ b/resources/views/post.blade.php @@ -105,19 +105,20 @@ class="col-lg-4 col-md-12 right-side d-flex justify-content-end"
05 Comments

-
+ @foreach ($post->comments as $comment) +
- + {{$comment->user->image}}
-
Emilly Blunt
-

December 4, 2017 at 3:12 pm

+
{{$comment->user->name}}
+

{{$comment->created_at->format('D, d M Y H:i')}}

- Never say goodbye till the end comes! + {{$comment->comment}}

@@ -128,7 +129,8 @@ class="single-comment justify-content-between d-flex"
-
+ @endforeach + {{--
@@ -173,53 +175,8 @@ class="single-comment justify-content-between d-flex" >
-
-
-
-
-
- -
-
-
Emilly Blunt
-

December 4, 2017 at 3:12 pm

-

- Never say goodbye till the end comes! -

-
-
-
- reply -
-
-
-
-
-
-
- -
-
-
Emilly Blunt
-

December 4, 2017 at 3:12 pm

-

- Never say goodbye till the end comes! -

-
-
-
- reply -
-
-
+
--}} +
@@ -227,22 +184,31 @@ class="single-comment justify-content-between d-flex"
-
-
Leave a Reply
-
-
- - Comment + @guest +
+

Please log in to comment

+
+ @else +
+
Leave a Reply
+
+
+
+ @csrf + + +
+
-
+ @endguest
diff --git a/resources/views/user/comments/index.blade.php b/resources/views/user/comments/index.blade.php new file mode 100644 index 0000000..20c7092 --- /dev/null +++ b/resources/views/user/comments/index.blade.php @@ -0,0 +1,166 @@ +@extends('layouts.backend.app') +@push('header') + + + +@endpush +@section('content') +
+ + +
+
+
+
+ @if ($errors->any()) + + @foreach ($errors->all() as $error) + + @endforeach + + @endif + +
+
+
+
+ Comments Table +
+
+ + + + + + + + + + + + + @foreach ($comments as $key => $comment) + + + + + + + + + @endforeach + + +
#CommentUserPostCreated_AtAction
{{$key+1}}{{$comment->comment}}{{$comment->user->name}}{{$comment->post->title}}{{$comment->created_at->diffForHumans()}} + +
+ +
+
+
+
+
+ +
+ @foreach ($comments as $comment) + + @endforeach + + + +
+ + + +@endsection + +@push('footer') + + + + + + + + + + + + + + + {!! Toastr::message() !!} +@endpush diff --git a/resources/views/user/index.blade.php b/resources/views/user/index.blade.php index 4a64cd2..7a5fd0b 100644 --- a/resources/views/user/index.blade.php +++ b/resources/views/user/index.blade.php @@ -1,24 +1,156 @@ -@extends('layouts.app') +@extends('layouts.backend.app') @section('content') -
-
-
-
-
{{ __('Dashboard') }}
+ + +
+ + +
+
+
+
+
+ +
+
+
Posts
+
1,012
+
+
+
+
+
-
- @if (session('status')) - - @endif +
+
+
+
+
+ +
+
+
Users
+
961
+
+
+
+
+
+ +
+
+
+
+
+ +
+
+
Comments
+
770
+
+
+
+
+
- You are logged in as {{Auth::user()->name}} +
+
+
+
+
+ +
+
+
Likes
+
7670
+
+
+
+
+ Recent Comments +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
#FirstLastHandle
1MarkOtto@mdo
2JacobThornton@fat
3Larrythe Bird@twitter
+
+
+
+ @endsection - diff --git a/routes/web.php b/routes/web.php index 7d3d10b..e9e60dd 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,7 @@ name('category.post'); Route::get('/search', 'HomeController@search')->name('search'); Route::get('/tag/{name}', 'HomeController@tagPosts')->name('tag.posts'); - +Route::post('/comment/{post}', 'CommentController@store')->name('comment.store'); // Admin //////////////////////////////////////////////////////////////////////// @@ -42,11 +43,15 @@ Route::resource('user', 'UserController')->except(['create', 'show', 'edit', 'store']); Route::resource('category', 'CategoryController')->except(['create', 'show', 'edit']); Route::resource('post', 'PostController'); + Route::get('/comments', 'CommentController@index')->name('comment.index'); + Route::delete('/comment/{id}', 'CommentController@destroy')->name('comment.destroy'); }); // User //////////////////////////////////////////////////////////////////////// Route::group(['prefix' => 'user', 'as' => 'user.', 'namespace' => 'User', 'middleware' => ['auth', 'user']], function () { Route::get('dashboard', 'DashboardController@index')->name('dashboard'); + Route::get('comments', 'CommentController@index')->name('comment.index'); + Route::delete('/comment/{id}', 'CommentController@destroy')->name('comment.destroy'); });