How to add pagination with union in Laravel 4 and Laravel 5 ?

How to add pagination with union in Laravel 4 and Laravel 5 ?

In this post we will give you information about How to add pagination with union in Laravel 4 and Laravel 5 ?. Hear we will give you detail about How to add pagination with union in Laravel 4 and Laravel 5 ?And how to use it also give you demo for it if it is necessary.

You are using union and you want to give pagination, but you can’t give. i also fetch that problem if you use union with pagination. you have to create pagination manually using Paginator Class. if you are using Laravel 4 then you can use Paginator Class library and if you use Laravel 5 then you have to give help of IlluminatePaginationLengthAwarePaginator Class function. you have to many times need give a manually pagination like if you are using having, union etc. now i am giving you example of Laravel 4 and Laravel 5 both you can do this way.

Laravel 5

namespace AppHttpControllers;

use Input;

use Request;

use View;

class AdminController extends Controller

{

public function index()

{

$page = Input::get('page', 1);

$paginate = 10;

$members = DB::table("members")

->select("id", "firstname", "lastname", "email")

->where("site_id", $id);

$data = DB::table("users")

->select("id", "firstname", "lastname", "email")

->where("site_id", $id)

->union($members)

->get();

$offSet = ($page * $paginate) - $paginate;

$itemsForCurrentPage = array_slice($data, $offSet, $paginate, true);

$data = new IlluminatePaginationLengthAwarePaginator($itemsForCurrentPage, count($data), $paginate, $page);

return View::make('index',compact('data'));

}

}


Laravel 4

Also see:Laravel 5 – elasticsearch with pagination example

namespace AppHttpControllers;

use Input;

use Request;

use View;

class AdminController extends Controller

{

public function index()

{

$page = Input::get('page', 1);

$paginate = 10;

$members = DB::table("members")

->select("id", "firstname", "lastname", "email")

->where("site_id", $id);

$users = DB::table("users")

->select("id", "firstname", "lastname", "email")

->where("site_id", $id)

->union($members)

->get();

$slice = array_slice($users, $paginate * ($page - 1), $paginate);

$data = Paginator::make($slice, count($users), $paginate);

return View::make('index',compact('data'));

}

}

you can use this example and you can create pagination, let’s you have to try……

Hope this code and post will helped you for implement How to add pagination with union in Laravel 4 and Laravel 5 ?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  32  =  42

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US