Select the first 10 rows - Laravel Eloquent

前端 未结 3 1325
情话喂你
情话喂你 2021-02-01 00:21

So far I have the following model:

class Listing extends Eloquent {
     //Class Logic HERE
}

I want a basic function that retrieves the first

3条回答
  •  被撕碎了的回忆
    2021-02-01 01:09

    First you can use a Paginator. This is as simple as:

    $allUsers = User::paginate(15);
    
    $someUsers = User::where('votes', '>', 100)->paginate(15);
    

    The variables will contain an instance of Paginator class. all of your data will be stored under data key.

    Or you can do something like:

    Old versions Laravel.

    Model::all()->take(10)->get();
    

    Newer version Laravel.

    Model::all()->take(10);
    

    For more reading consider these links:

    • pagination docs
    • passing data to views
    • Eloquent basic usage
    • A cheat sheet

提交回复
热议问题