Skip to content

Instantly share code, notes, and snippets.

@lucenarenato
Forked from nellytadi/manual_paginate.php
Created October 14, 2022 21:47
Show Gist options
  • Select an option

  • Save lucenarenato/2207495bed698bfc624882a8ee3e507c to your computer and use it in GitHub Desktop.

Select an option

Save lucenarenato/2207495bed698bfc624882a8ee3e507c to your computer and use it in GitHub Desktop.
Manual Paginate In Laravel
<?php
namespace App\Http\Controllers\Api;
use App\Model;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
class TestController extends Controller
{
public function test(Request $request){
$array = Model::select('id','organisation_name','company_status','created_at','updated_at')->where('publish', 1)->limit(15)->get();
$total=count($array);
$per_page = 5;
$current_page = $request->input("page") ?? 1;
$starting_point = ($current_page * $per_page) - $per_page;
$array = array_slice($array, $starting_point, $per_page, true);
$array = $array->toArray();
$array = array_slice($array, $starting_point, $per_page, true);
$array = new Paginator($array, $total, $per_page, $current_page, [
'path' => $request->url(),
'query' => $request->query(),
]);
dd($array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment