Skip to content

Commit

Permalink
Comment system done
Browse files Browse the repository at this point in the history
  • Loading branch information
subhadipghorui committed Oct 7, 2020
1 parent 500965e commit 41f601a
Show file tree
Hide file tree
Showing 13 changed files with 678 additions and 82 deletions.
17 changes: 17 additions & 0 deletions app/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
public function post()
{
return $this->belongsTo('App\Post');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
24 changes: 24 additions & 0 deletions app/Http/Controllers/Admin/CommentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Comment;
use App\Http\Controllers\Controller;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;

class CommentController extends Controller
{
public function index()
{
$comments = Comment::all();
return view('admin.comments.index', compact('comments'));
}
public function destroy($id)
{
$comment = Comment::findOrFail($id);
$comment->delete();
Toastr::success('Comment successfully deleted :)');
return redirect()->back();
}
}
25 changes: 25 additions & 0 deletions app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Http\Controllers;

use App\Comment;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CommentController extends Controller
{
public function store(Request $request, $post)
{
$this->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();
}
}
30 changes: 30 additions & 0 deletions app/Http/Controllers/User/CommentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Controllers\User;

use App\Comment;
use App\Http\Controllers\Controller;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CommentController extends Controller
{
public function index()
{
$comments = Comment::where('user_id', Auth::id())->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();
}
}
}
4 changes: 4 additions & 0 deletions app/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ public function posts()
{
return $this->hasMany('App\Post');
}
public function comments()
{
return $this->hasMany('App\Comment');
}
}
34 changes: 34 additions & 0 deletions database/migrations/2020_10_07_072827_create_comments_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->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');
}
}
166 changes: 166 additions & 0 deletions resources/views/admin/comments/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
@extends('layouts.backend.app')
@push('header')
<link rel="stylesheet" href="{{asset('backend/vendors/datatables.net-bs4/css/dataTables.bootstrap4.min.css')}}" />
<link rel="stylesheet" href="{{asset('backend/vendors/datatables.net-buttons-bs4/css/buttons.bootstrap4.min.css')}}" />
<link rel="stylesheet" href="http://cdn.bootcss.com/toastr.js/latest/css/toastr.min.css">
@endpush
@section('content')
<div id="right-panel" class="right-panel">
<div class="breadcrumbs">
<div class="col-sm-4">
<div class="page-header float-left">
<div class="page-title">
<h1>Comments</h1>
</div>
</div>
</div>
<div class="col-sm-8">
<div class="page-header float-right">
<div class="page-title">
<ol class="breadcrumb text-right">
<li><a href="#">Dashboard</a></li>
<li>
<a href="#" class="active">Comments</a>
</li>
</ol>
</div>
</div>
</div>
</div>

<div class="content mt-3">
<div class="animated fadeIn">
<div class="row">
<div class="col-md-12">
@if ($errors->any())

@foreach ($errors->all() as $error)
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<span class="badge badge-pill badge-danger">Erorr</span> {{$error}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@endforeach

@endif

</div>
<div class="col-md-12">
<div class="card">
<div class="card-header">
<strong class="card-title">Comments Table</strong>
</div>
<div class="card-body">
<table id="bootstrap-data-table-export" class="table table-striped table-bordered">
<thead>
<tr>
<th>#</th>
<th>Comment</th>
<th>User</th>
<th>Post</th>
<th>Created_At</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach ($comments as $key => $comment)
<tr>
<td>{{$key+1}}</td>
<td>{{$comment->comment}}</td>
<td>{{$comment->user->name}}</td>
<td><a href="{{route('post',$comment->post->slug)}}">{{$comment->post->title}}</a></td>
<td>{{$comment->created_at->diffForHumans()}}</td>
<td>
<button type="button" class="btn btn-danger mb-1" data-toggle="modal"
data-target="#deleteModal-{{$comment->id}}">
<i class="fa fa-trash-o"></i>
</button>
</td>
</tr>
@endforeach

</tbody>
</table>

</div>
</div>
</div>
</div>
</div>
<!-- .animated -->
<div class="animated">
@foreach ($comments as $comment)
<div class="modal fade" id="deleteModal-{{$comment->id}}" tabindex="-1" role="dialog" aria-labelledby="staticModalLabel"
data-backdrop="static" aria-hidden="true" style="display: none;">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticModalLabel">Delete Comment</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>
The Comment will be deleted !!
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-danger" onclick="event.preventDefault();
document.getElementById('deletecomment-{{$comment->id}}').submit();">Confirm</button>
<form action="{{route('admin.comment.destroy', $comment->id)}}" style="display: none" id="deletecomment-{{$comment->id}}" method="POST">
@csrf
@method('DELETE')
</form>
</div>
</div>
</div>
</div>
@endforeach


<!-- .content -->
</div>


<!-- .content -->
@endsection

@push('footer')

<script src="{{asset('backend/vendors/datatables.net/js/jquery.dataTables.min.js')}}"></script>
<script src="{{asset('backend/vendors/datatables.net-bs4/js/dataTables.bootstrap4.min.js')}}"></script>
<script src="{{asset('backend/vendors/datatables.net-buttons/js/dataTables.buttons.min.js')}}"></script>
<script src="{{asset('backend/vendors/datatables.net-buttons-bs4/js/buttons.bootstrap4.min.js')}}"></script>
<script src="{{asset('backend/vendors/jszip/dist/jszip.min.js')}}"></script>
<script src="{{asset('backend/vendors/pdfmake/build/pdfmake.min.js')}}"></script>
<script src="{{asset('backend/vendors/pdfmake/build/vfs_fonts.js')}}"></script>
<script src="{{asset('backend/vendors/datatables.net-buttons/js/buttons.html5.min.js')}}"></script>
<script src="{{asset('backend/vendors/datatables.net-buttons/js/buttons.print.min.js')}}"></script>
<script src="{{asset('backend/vendors/datatables.net-buttons/js/buttons.colVis.min.js')}}"></script>
<script src="{{asset('backend/assets/js/init-scripts/data-table/datatables-init.js')}}">
<script>
$(document).ready(function () {
(function ($) {
$('#filter').keyup(function () {
var rex = new RegExp($(this).val(), 'i');
$('.searchable tr').hide();
$('.searchable tr').filter(function () {
return rex.test($(this).text());
}).show();
})
}(jQuery));
});
</script>
<script src="http://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/toastr.js/latest/js/toastr.min.js"></script>
{!! Toastr::message() !!}
@endpush
23 changes: 23 additions & 0 deletions resources/views/layouts/backend/partials/navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class="navbar-toggler"
</div>

<div id="main-menu" class="main-menu collapse navbar-collapse">
@if (Auth::user()->role_id==1)
<ul class="nav navbar-nav">
<li class="active">
<a href="{{route('admin.dashboard')}}">
Expand All @@ -45,7 +46,29 @@ class="navbar-toggler"
<i class="menu-icon fa fa-file"></i>Posts
</a>
</li>
<li class="active">
<a href="{{route('admin.comment.index')}}">
<i class="menu-icon fa fa-file"></i>Comments
</a>
</li>
</ul>
@else
<ul class="nav navbar-nav">
<li class="active">
<a href="{{route('user.dashboard')}}">
<i class="menu-icon fa fa-dashboard"></i
>Dashboard
</a>
</li>
<h3 class="menu-title">CMS</h3>
<!-- /.menu-title -->
<li class="active">
<a href="{{route('user.comment.index')}}">
<i class="menu-icon fa fa-file"></i>Comments
</a>
</li>
</ul>
@endif
</div>
<!-- /.navbar-collapse -->
</nav>
Expand Down
Loading

0 comments on commit 41f601a

Please sign in to comment.