Laravel Sort在分页后附加日期

时间:2019-11-13 11:17:12

标签: php laravel

我的代码在此处按dateOrder排序分页,并且dateOrder在“我的模型”中,追加如何不对每30条记录的所有结果进行排序

$posts = Post::paginate(30)->sortByDesc('dateOrder');



protected $appends =['dateOrder'];

public function getdateOrderAttribute()
{
    if($this->add_date && is_null($this->updated_at)  )
        return Carbon::createFromTimestamp($this->add_date)->toDateTimeString();

    return ($this->updated_at)->toDateTimeString();

}

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

$posts = Post::paginate(30)->sortByDesc('date_order');



protected $appends =['dateOrder'];

public function getdateOrderAttribute()
{
    if($this->add_date && is_null($this->updated_at)  ){
        return Carbon::createFromTimestamp($this->add_date)->toDateTimeString();
    } else {
        return $this->updated_at->toDateTimeString();
    }

}

我希望它会有所帮助。