简体   繁体   English

Laravel 5.5-未找到方法分页

[英]Laravel 5.5 - Method paginate not found

I don't know what's wrong, but here's my script at my AppController. 我不知道出了什么问题,但这是我在AppController上的脚本。

function getData () {
    $list_data = MyModel::all()->sortBy('id')->paginate(15);
    $count_data = $list_siswa->count();
    return view('pages.list', ['list' => $list_data, 'count' => $count_data]);
}

And here's my model 这是我的模特

class MyModel extends Model {
    protected $table = 'students';

    protected $fillable = [
        'id',
        'name',
        'class',
        'gender',
        'address'
    ];
}

Any idea? 任何想法? I think the problem is in my controller. 我认为问题出在我的控制器上。

You must paginate a database query not a collection, therefore you must user orderBy instead of combining all with sortBy , I have tested the below code and can confirm it works 您必须对数据库查询而不是集合进行分页,因此您必须使用orderBy而不是将allsortBy结合使用,我已经测试了以下代码,可以确认它是否有效

function getData () {
    $list_data = MyModel::orderBy('id')->paginate(15);
    $count_data = $list_data->count();
    return view('pages.list', ['list' => $list_data, 'count' => $count_data]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM