Laravel 5.1多次订购

时间:2016-01-21 18:00:26

标签: php laravel laravel-5.1

我试图制作像Facebook一样的东西...... 现在,我想使用posts表订购created_at。但是,如果帖子有评论,请按评论排序。实际上,帖子需要通过评论排序,如果帖子没有评论,那么它需要通过帖子排序(作为后备)。

为了清楚说明,我制作了一些截图:

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

上找到演示

2 个答案:

答案 0 :(得分:0)

您可以轻松使用“触摸父时间戳”的概念,请在此处详细了解https://laravel.com/docs/5.1/eloquent-relationships#touching-parent-timestamps

但是这里有一个简单的解释,因为评论与帖子有关,你可以指示评论模型在添加或甚至编辑评论时更新父帖子的updated_at字段,所以现在你有了帖子的updated_at始终是最新的。

答案 1 :(得分: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对象,这将变得更加容易。