【问题标题】:Fetching post comment order by in laravel在laravel中获取帖子评论顺序
【发布时间】:2017-03-03 21:07:53
【问题描述】:

我想通过 created at 获取评论顺序,但我该怎么做? 我照常做,但没有用。 @foreach( $post->comnt->take(1)->orderBy('created_at','DESC') as $cmets )

                    @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');
}

}

【问题讨论】:

  • 显示型号代码

标签: laravel


【解决方案1】:

也许

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 2018-12-23
    • 2014-05-10
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多