I'm trying to make something 'like facebook'... Now, I want to order the posts
using the created_at
table. But also, if the post has a comment, order it by the comments. So actually, the posts need to be ordered by the comment and if the post doesn't have a comment, then it needs to order by the post (acting as a fallback).
To make it all clear, I have made some screenshots:
So this is just one post, nothing to mention about it.
But when I add a second post with 456
as content, that post needs to be on top, wich works:
All good, but now, when I add a comment, the message with the comment needs to be on top (since it has a newer post). This is my problem, it doesn't work:
So here is my code:
Controller:
public function index()
{
$getallposts = DB::table('social_posts')->select('users.*', 'social_posts.created_at as PostAt', 'social_posts.description', 'users_images.*', 'social_posts.id as PostID')
->join('users', 'social_posts.user_id', '=', 'users.id')
->join('users_images', 'social_posts.user_id', '=', 'users_images.user_id')
->orderBy('social_posts.created_at', 'DESC')
->whereNull('social_posts.deleted_at')
->get();
//var_dump($getallposts);
$getallcomments = DB::table('social_comments')->select('users.*', 'social_comments.created_at as PostAt', 'social_comments.description', 'social_comments.post_id', 'users_images.*')
->join('users', 'social_comments.user_id', '=', 'users.id')
->join('users_images', 'social_comments.user_id', '=', 'users_images.user_id')
->orderBy('social_comments.created_at', 'ASC')
->whereNull('social_comments.deleted_at')
->get();
//var_dump($getallposts);
$getrandomusers = DB::table('users')->select('users.*', 'users.id as UID', 'users_images.*')
->join('users_images', 'users.id', '=', 'users_images.user_id')
->orderByRaw('RAND()')
->where('users.id', '!=', Auth::id())
->take(16)
->get();
return view('dashboard', ['posts' => $getallposts, 'comments' => $getallcomments, 'randomuser' => $getrandomusers]);
}
View:
@foreach($posts as $post)
<div class="row row-sm">
<div class="col-sm-12">
<div class="card">
<div class="card-heading">
<a href class="pull-left w-32 m-r" href="{!! url(Auth::user()->slug) !!}">
<img src="{!! asset('avatars/'.$post->image) !!}" class="w-full img-circle">
</a>
<div class="clear">
<a href="{!! url($post->slug) !!}" class="font-bold block">{!! ucwords($post->firstname) !!} {!! ucwords($post->lastname) !!}</a>
<div class="text-xxs font-thin text-muted">{!! \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $post->PostAt)->diffForHumans() !!}</div>
</div>
</div>
<div class="card-body">
<p>
{!! nl2br(e($post->description)) !!}
</p>
<p style="color:grey;font-size:10px;">Aantal likes - {!! \Cussion\SocialReaction::where('post_id', $post->PostID)->count() !!} {!! (\Cussion\SocialReaction::where('post_id', $post->PostID)->count() == 1) ? 'reactie' : 'reacties' !!} </p>
<p style="font-size:14px;">Leuk vinden</p> <!-- KNOP OM STATUS TE LIKEN -->
</div>
<div class="list-group no-radius no-border" style="background-color:#F5F5F5;">
@foreach($comments as $comment)
@if($comment->post_id == $post->PostID)
<div class="md-list-item">
<div class="md-list-item-left">
<img src="{!! asset('avatars/'.$comment->image) !!}" class="w-full circle">
</div>
<div class="md-list-item-content">
<small class="font-thin">{!! ucwords($comment->firstname) !!} {!! ucwords($comment->lastname) !!}</small>
<div class="text-xxs font-thin text-muted" style="font-size:12px;">{!! nl2br(e($comment->description)) !!}</div>
<div class="text-xxs font-thin text-muted" style="font-size:10px;">{!! \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $comment->PostAt)->diffForHumans() !!}</div>
</div>
</div>
@endif
@endforeach
<div class="md-list-item">
<form action="{!! url('/dashboard') !!}" method="post" role="form">
{!! csrf_field() !!}
<div class="input-group">
<input type="text" class="form-control" name="message" placeholder="Wat wil je reageren?">
<input type="hidden" name="post_id" value="{!! $post->PostID !!}">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Reageer</button>
</span>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endforeach
And my database structure:
Demo can be found on http://cussion.org
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire