2016-01-21 73 views
0

我試圖使一些「像Facebook」 ... 現在,我想用created_at表訂購posts。而且,如果該帖子有評論,請按評論排序。所以實際上,這些帖子需要通過評論排序,如果帖子沒有評論,那麼它需要通過帖子排序(作爲回退)。Laravel 5.1排序多次

,使這一切清楚了,我已經取得了一些截圖:

1

所以這只是一個職位,沒有提這件事。

但是當我添加第二個職位與456的內容,該職位需要在最前面,至極的作品: 2

都好,但現在,當我添加註釋,該註釋信息需要處於頂端(因爲它有一個更新的帖子)。這是我的問題,這是行不通的:

3

因此,這裏是我的代碼:

控制器:

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 

和我數據庫結構:

4

演示可以http://cussion.org

+0

我認爲這是一個更好的選擇,在Posts模型中創建一個「last_reply_at」字段,然後在每次發表評論時更新此字段並使用此字段進行排序。 –

回答

0

找到您可以輕鬆使用的「感人父時間戳」的概念,閱讀更多關於它在這裏https://laravel.com/docs/5.1/eloquent-relationships#touching-parent-timestamps

但這裏是一個簡單的解釋,因爲評論與帖子相關,無論何時添加評論或編輯評論,都可以指示評論模型更新父帖子的updated_at字段,所以現在您的帖子的updated_at始終保持最新。

+0

我可能會使用此... – Robin

0

我會推遲排序到您的PHP腳本。在MySQL中編寫這種排序算法可能會變得非常複雜,難以閱讀。相反,我會將結果存儲在一個集合中,然後使用自定義函數進行排序。這樣的事情:

$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') 
    ->whereNull('social_posts.deleted_at') 
    ->get(); 

$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(); 

//add comments to their posts and put post 
foreach($getallposts as $post){ 
    $post->comments = []; 
    foreach($getallcomments as $comment){ 
     if($comment->post_id == $post->PostID){ 
      $post->comments[] = $comment; 
     } 
    } 
} 

$getallposts->sortBy(function($item, $key){ 
    //run your sorting logic here based on comments and created_at date 
}); 

更好的是,將你的sql查詢轉換爲Eloquent對象,這會變得更容易。

+0

如果我想把它們轉換成雄辯的對象(我其實更喜歡),我該怎麼做?因爲我真的不知道我應該如何明星。 – Robin

+0

從這裏開始:https://laravel.com/docs/eloquent。從本質上講,您可以像創建任何其他PHP對象一樣創建類,並定義一些您將在文檔中看到的內容。然後你可以像這樣調用你的帖子:'$ posts = Posts :: all();'可以在這裏查看帖子的評論:'$ posts [0] - > comments'。 –

+0

我也建議檢查laracasts。有些課程是免費的,但訂閱價值10美元/月:https://laracasts.com/series/laravel-5-fundamentals/episodes/8 –