Skip to content

Commit

Permalink
Liked and Views Complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
subhadipghorui committed Oct 24, 2020
1 parent 750f89b commit 20e97c5
Show file tree
Hide file tree
Showing 16 changed files with 355 additions and 20 deletions.
4 changes: 4 additions & 0 deletions app/Http/Controllers/Admin/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,8 @@ public function destroy($id)

return redirect()->route('admin.post.index');
}
public function likedUsers($post){
$post = Post::findOrFail($post);
return view('admin.post.likedUsers',compact('post'));
}
}
24 changes: 22 additions & 2 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\Post;
use App\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

class HomeController extends Controller
{
Expand All @@ -28,8 +30,15 @@ public function posts()
public function post($slug)
{
$post = Post::where('slug', $slug)->published()->first();
$posts = Post::latest()->take(3)->published()->get();
return view('post', compact('post', 'posts'));
// $posts = Post::latest()->take(3)->published()->get();
// Increase View count
$postKey = 'post_'.$post->id;
if(!Session::has($postKey)){
$post->increment('view_count');
Session::put($postKey, 1);
}

return view('post', compact('post'));
}
public function categories()
{
Expand Down Expand Up @@ -61,4 +70,15 @@ public function tagPosts($name)

return view('tagPosts', compact('tags', 'query'));
}
public function likePost($post){
// Check if user already liked the post or not
$user = Auth::user();
$likePost = $user->likedPosts()->where('post_id', $post)->count();
if($likePost == 0){
$user->likedPosts()->attach($post);
} else{
$user->likedPosts()->detach($post);
}
return redirect()->back();
}
}
4 changes: 4 additions & 0 deletions app/Http/Controllers/User/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

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

class DashboardController extends Controller
{
public function index()
{
return view('user.index');
}
public function likedPosts(){
return view('user.likedPosts');
}
}
6 changes: 6 additions & 0 deletions app/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ public function scopePublished($query)
{
return $query->where('status', 1);
}

// many to many
public function likedUsers()
{
return $this->belongsToMany('App\User')->withTimestamps();
}
}
5 changes: 5 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public function comments()
public function replies(){
return $this->hasMany('App\CommentReply');
}
// Many to many
public function likedPosts()
{
return $this->belongsToMany('App\Post')->withTimestamps();
}
}
33 changes: 33 additions & 0 deletions database/migrations/2020_10_24_190145_create_post_user_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

class CreatePostUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('post_user', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('user_id');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('post_user');
}
}
4 changes: 2 additions & 2 deletions resources/views/admin/post/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
<th>#</th>
<th>Title</th>
<th>Slug</th>
<th>Views & Likes</th>
<th>Created_At</th>
<th>Updated_At</th>
<th>Action</th>
</tr>
</thead>
Expand All @@ -70,8 +70,8 @@
<td>{{$key+1}}</td>
<td>{{$post->title}}</td>
<td>{{$post->slug}}</td>
<td><a href="{{route('admin.post.like.users', $post->id)}}" class="btn btn-danger" type="button"> <i class="fa fa-heart"></i> {{$post->likedUsers->count()}}</a> <button class="btn btn-info" type="button"><i class="fa fa-eye"></i> {{$post->view_count}}</button></td>
<td>{{$post->created_at}}</td>
<td>{{$post->updated_at}}</td>
<td>
<!-- Button trigger modal -->
<a href="{{route('admin.post.show', $post->id)}}" class="btn btn-info"><i class="fa fa-eye"></i></a>
Expand Down
122 changes: 122 additions & 0 deletions resources/views/admin/post/likedUsers.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
@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>Post</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">Post Table</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">{{$post->title}} Liked User Table</strong>

</div>
<div class="card-body">
<table id="bootstrap-data-table-export" class="table table-striped table-bordered">
<thead>
<tr>
<th>#</th>
<th>User Name</th>
<th>User Id</th>
</tr>
</thead>
<tbody>
@foreach ($post->likedUsers as $key => $likeUser)
<tr>
<td>{{$key+1}}</td>
<td>{{$likeUser->name}}</td>
<td>{{$likeUser->id}}</td>
</tr>
@endforeach

</tbody>
</table>

</div>
</div>
</div>
</div>
</div>
</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
6 changes: 3 additions & 3 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ class="col-lg-6 flex-row d-flex meta-right no-padding justify-content-end"
<p class=" footer"="">
<br>
</p><ul class="d-flex space-around">
<li><a href="javascript:void(0);" onclick=" toastr.info('To add to your favorite list you have to login first.', 'Info', { closeButton: true, progressBar: true, })"><i class="fa fa-heart-o" aria-hidden="true"></i><span> 0</span></a></li>
<li><a href="javascript:void(0);" onclick=" toastr.info('To add to your favorite list you have to login first.', 'Info', { closeButton: true, progressBar: true, })"><i class="fa fa-heart-o" aria-hidden="true"></i><span> {{$post->likedUsers->count()}}</span></a></li>


<li><i class="fa fa-comment-o" aria-hidden="true"></i><span> 0</span></li>
<li><i class="fa fa-eye" aria-hidden="true"></i> <span>5</span></li>
<li><i class="fa fa-comment-o" aria-hidden="true"></i><span> {{$post->comments->count()}}</span></li>
<li><i class="fa fa-eye" aria-hidden="true"></i> <span>{{$post->view_count}}</span></li>
</ul>

<p></p>
Expand Down
3 changes: 1 addition & 2 deletions resources/views/layouts/frontend/partials/navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@
<a class="dropdown-item" href="{{ route('admin.dashboard') }}"><i class="fa fa-tv" aria-hidden="true"></i>&nbsp; Dashboard</a>
@elseif(Auth::user()->role->id == 2)
<a class="dropdown-item" href="{{ route('user.dashboard') }}"><i class="fa fa-tv" aria-hidden="true"></i>&nbsp; Dashboard</a>
<a class="dropdown-item" href="{{route('user.like.posts')}}"><i class="fa fa-heart" aria-hidden="true"></i>&nbsp; Favorite List</a>
@else
null
@endif

<a class="dropdown-item" href="/admin/dashboard"><i class="fa fa-heart" aria-hidden="true"></i>&nbsp; Favorite List</a>
<a class="dropdown-item" href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Expand Down
22 changes: 17 additions & 5 deletions resources/views/post.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,27 @@ class="col-lg-4 col-md-12 right-side d-flex justify-content-end"
</div>
<div class="bottom-wrapper">
<div class="row">
<div class="col-lg-4 single-b-wrap col-md-12">
<i class="fa fa-heart-o" aria-hidden="true"></i>
lily and 4 people like this
<div class="col-lg-3 single-b-wrap col-md-12">
@guest
<i class="fa fa-heart-o" aria-hidden="true"></i>{{$post->likedUsers->count()}} people like this
@else
<a href="#" onclick="document.getElementById('like-form-{{$post->id}}').submit();"> <i class="fa fa-heart" aria-hidden="true" style="color: {{Auth::user()->likedPosts()->where('post_id', $post->id)->count() > 0 ? 'red' : ''}}"></i></a>
{{$post->likedUsers->count()}} people like this

<form action="{{route('post.like',$post->id)}}" method="POST" style="display: none" id="like-form-{{$post->id}}">
@csrf
</form>
@endguest
</div>
<div class="col-lg-3 single-b-wrap col-md-12">
<i class="fa fa-eye" aria-hidden="true"></i> {{$post->view_count}}
views
</div>
<div class="col-lg-4 single-b-wrap col-md-12">
<div class="col-lg-3 single-b-wrap col-md-12">
<i class="fa fa-comment-o" aria-hidden="true"></i> {{$post->comments->count()}}
comments
</div>
<div class="col-lg-4 single-b-wrap col-md-12">
<div class="col-lg-3 single-b-wrap col-md-12">
<ul class="social-icons">
<li>
<a href="#"
Expand Down
4 changes: 2 additions & 2 deletions resources/views/posts.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
</p>
<p class="footer pt-20">
<i class="fa fa-heart-o" aria-hidden="true"></i>
<a href="#">06 Likes</a>
<a href="#">{{$post->likedUsers->count()}} Likes</a>
<i
class="ml-20 fa fa-comment-o"
aria-hidden="true"
></i>
<a href="#">02 Comments</a>
<a href="#">{{$post->comments->count()}} Comments</a>
</p>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/search.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
</p>
<p class="footer pt-20">
<i class="fa fa-heart-o" aria-hidden="true"></i>
<a href="#">06 Likes</a>
<a href="#">{{$post->likedUsers->count()}} Likes</a>
<i
class="ml-20 fa fa-comment-o"
aria-hidden="true"
></i>
<a href="#">02 Comments</a>
<a href="#">{{$post->comments->count()}} Comments</a>
</p>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/tagPosts.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@
</p>
<p class="footer pt-20">
<i class="fa fa-heart-o" aria-hidden="true"></i>
<a href="#">06 Likes</a>
<a href="#">{{$tag->post->likedUsers->count()}} Likes</a>
<i
class="ml-20 fa fa-comment-o"
aria-hidden="true"
></i>
<a href="#">02 Comments</a>
<a href="#">{{$tag->post->comments->count()}} Comments</a>
</p>
</div>
</div>
Expand Down
Loading

0 comments on commit 20e97c5

Please sign in to comment.