In Laravel 5.0 we have "paginate" in Eloquent by which we can create pagination very easily by just following some simple steps.However there is a drawback with it as it only works if you know all the conditions prior. But what if you want to create pagination related to the dynamic conditions i.e. as per the end users expectation.
Hence we can use Paginator and LengthAwarePaginator classes to accomplish that.
Please follow the following steps to accomplish it:-
Step 1st:- use DB in the modal.
use DB;
Step 2nd:- Create a method in modal.
public function getNeedList($companyId, $condition){
$query = "SELECT need.*, user.*,........"
. " FROM needs AS need "
. " INNER JOIN userinfo AS user on need.userid = user.userid "
. " INNER JOIN .............."
. " INNER JOIN .............. "
. " WHERE company_id = {$companyId}"
. " and need.status = 1 {$condition}";
$all_transactions = DB::select(DB::raw($query));
return $all_transactions;
}
Step 3rd:- In the controller use the following classes.
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
Step 4th:- Add the following code in the controller method.
// $condition is a string clubbed with all the conditions end user want to search for.
$result = $need->getNeedList2($companyInfo[0]['id'], $condition);
$data = array_slice($result, $limit * (Input::get('page', 1) - 1), $limit);
$paginator = new LengthAwarePaginator($result, count($result), $limit, Paginator::resolveCurrentPage(), ['path' => Paginator::resolveCurrentPath()]);
return view('view_name...', compact('paginator', .................));
Step 5th:- Add the following code in the HTML
<?php echo $paginator->appends($parameters)->render(); ?>
2 Comment(s)