提问者:小点点

获取laravel中的post comment order by


我想获取由创建的评论顺序,但我怎么能做到这一点?我照常按命令做了,但没有成功。@Foreach($post-

                    @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个答案

匿名用户

大概

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