通过laravel获取发表评论顺序

时间:2016-10-21 13:58:37

标签: laravel

我想通过创建来获取评论顺序,但我该怎么做? 我照常做的顺序,但它没有奏效。 @foreach($ post-> comnt-> take(1) - > orderBy('created_at','DESC')as $ comments)

                    @if($post->comnt)

                    <div class="comments clearfix">
                    @foreach( $post->comnt->take(1) as $comments )
                        <div class="each_coments clearfix">
                            <p> <span class="comment_profile"><img src="{{ asset('img').'/'.$comments->user->image }}" alt=""></span></p>
                            <p><a href=" {{ route('postUserinfo', [ 'id' => $post->user_id ]) }} ">{{ $comments->user->username }}</a>{{ $comments->comment }}</p>
                        </div><?php $last_id = $comments->id ?>
                    @endforeach
                        <a data-lastid="@if(!empty($last_id)){{$last_id}}@else{{'0'}}@endif" href="">lode more comments</a>
                    </div>
                    <?php unset($last_id); ?>
                    @endif

这是我的帖子模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{

protected $table = 'posts';

public function user()
{
    return $this->belongsTo('App\User');
}

public function likes()
{
    return $this->hasMany('App\like');
}

public function comnt()
{
    return $this->hasMany('App\comment');
}

}

1 个答案:

答案 0 :(得分:1)

也许

public function comnt()
{
    return $this->hasMany('App\comment')->orderBy('created_at','DESC');
}