我試圖使一些「像Facebook」 ... 現在,我想用created_at
表訂購posts
。而且,如果該帖子有評論,請按評論排序。所以實際上,這些帖子需要通過評論排序,如果帖子沒有評論,那麼它需要通過帖子排序(作爲回退)。Laravel 5.1排序多次
,使這一切清楚了,我已經取得了一些截圖:
所以這只是一個職位,沒有提這件事。
但是當我添加第二個職位與456
的內容,該職位需要在最前面,至極的作品:
都好,但現在,當我添加註釋,該註釋信息需要處於頂端(因爲它有一個更新的帖子)。這是我的問題,這是行不通的:
因此,這裏是我的代碼:
控制器:
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]);
}
查看:
@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
和我數據庫結構:
我認爲這是一個更好的選擇,在Posts模型中創建一個「last_reply_at」字段,然後在每次發表評論時更新此字段並使用此字段進行排序。 –