laravel 4 paginate collection

后端 未结 3 1610
天涯浪人
天涯浪人 2021-01-06 14:30

I cant create a proper pagination system using laravel 4. I have the following models and function that return collections:

Model Restaurant:

public          


        
3条回答
  •  猫巷女王i
    2021-01-06 14:49

    The paginator must get the items that it would normally get from a database query with an offset/limit statement. So when you have a collection with all items, you should do the offset/limit yourself.

        $food = $food->get_rest_foods($id);
    
        $page = 1;
        if( !empty(Input::get['page']) ) {
            $page = Input::get['page'];
        }
    
        $perPage = 15;
        $offset = (($page - 1) * $perPage);
    
        $food = Paginator::make($food->slice($offset,$perPage, true)->all(), $food->count(), $perPage);
    

提交回复
热议问题