Laravel 4.2 Pagination with complex DB query - php

I am new to Laravel 4.2.
I need to do some pagination in my view search result page.
What I am doing is writing this code in the controller -
public function getSpecials()
{
$title = "Specials";
$info = DB::table(DB::raw('`car` , `available_car`, `users`'))
->select('car.car_maker', 'car.car_model', 'available_car.car_price', 'car.car_production_year', 'available_car.car_id', 'available_car.id', 'available_car.current_position')
->where('available_car.car_id', '`car`.`car_id`')
->where('available_car.is_sold', 'no')
->whereRaw('`available_car`.`created_at` BETWEEN NOW() - INTERVAL 30 DAY AND NOW()')
->orderByRaw('WEEK(`available_car`.`created_at`) DESC')
->orderBy('available_car.car_price', 'desc')
->orderBy('users.last_paid_date', 'desc')
->orderBy('available_car.created_at', 'desc')
->distinct()
->get();
$pagination = Paginator::make($info, count($info), 5);
//var_dump($pagination );
return View::make('specials',compact('pagination'))->with('info',$info)->with('title',$title);
}
I want to show 5 items per page. So I am doing paginate like this -
$pagination = Paginator::make($info, count($info), 5);
But the problem here is I am getting paginate number in the page perfectly.
But, All page is showing the whole result, not showing only 5 items.
It is the output. (paginate is for 5 items per page and total no of entry is 7 in my case)
Page 1
Page 2
Can anyone help me please?
Thanks in advance for helping.

just make a small change in the code and laravel will take care of the rest.
public function getSpecials()
{
$title = "Specials";
$info = DB::table(DB::raw('`car` , `available_car`, `users`'))
->select('car.car_maker', 'car.car_model', 'available_car.car_price', 'car.car_production_year', 'available_car.car_id', 'available_car.id', 'available_car.current_position')
->where('available_car.car_id', '`car`.`car_id`')
->where('available_car.is_sold', 'no')
->whereRaw('`available_car`.`created_at` BETWEEN NOW() - INTERVAL 30 DAY AND NOW()')
->orderByRaw('WEEK(`available_car`.`created_at`) DESC')
->orderBy('available_car.car_price', 'desc')
->orderBy('users.last_paid_date', 'desc')
->orderBy('available_car.created_at', 'desc')
->distinct()
->paginate(5);
return View::make('specials',['info' => $info, 'title' => $title]);
}
in view:
Iterate like this:
#if(!$info->isEmpty())
#foreach($info as $i)
do whatever you want
#endforeach
#else
no data to show....
#endif
{{$info->links()}}

You need to create your paginator manually (Check this question for more details):
$pageNumber = Input::get('page', 1);
$perPage = 5;
$info = DB::table(DB::raw('`car` , `available_car`, `users`'))
->select('car.car_maker', 'car.car_model', 'available_car.car_price', 'car.car_production_year', 'available_car.car_id', 'available_car.id', 'available_car.current_position')
->where('available_car.car_id', '`car`.`car_id`')
->where('available_car.is_sold', 'no')
->whereRaw('`available_car`.`created_at` BETWEEN NOW() - INTERVAL 30 DAY AND NOW()')
->orderByRaw('WEEK(`available_car`.`created_at`) DESC')
->orderBy('available_car.car_price', 'desc')
->orderBy('users.last_paid_date', 'desc')
->orderBy('available_car.created_at', 'desc')
->distinct()
->get();
$slice = array_slice($info, $perpage * ($pageNumber - 1), $perpage);
$info = Paginator::make($slice, count($info), $perPage);
return View::make('specials',['info' => $info, 'title' => $title]);
Then print results in a foreach loop like suggested by #itachi.

Related

How to limit amount of pages in laravel pagination?

I have a pagination and try to limit the amount of pages for that pagination. Every page has 50 results and the maximum amount of pages I want for this pagination is 200 (10k total entries from the table). I tried the following:
$page_count = 50;
$users = DB::Table('user_totalpoints')->where('points', '>', 0)
->orderBy('points', 'desc')
->orderBy('id', 'asc')
->take(10000)
->paginate($page_count);
The result of this is a pagination with 50 results per page but with ALL entries from the user_totalpoints table. take(10000) is ignored as there are only 50 taken by the pagination. So instead of maximum 200pages I now have thousands.
How can I limit the amount of pages for my pagination in laravel?
Manual Paginator LengthAwarePaginator
// limit page max to 200 and min to 1
$this->validate($request, [
'page' => 'required|integer|min:1|max:200'
]);
$page = $request->get('page');
$page_count = 50;
$total = 10000;
$users = DB::Table('user_totalpoints')->where('points', '>', 0)
->orderBy('points', 'desc')
->orderBy('id', 'asc')
->forPage($page, $page_count)
->get();
/**
* use Illuminate\Pagination\LengthAwarePaginator;
*/
$paginator = new LengthAwarePaginator($users, $total, $page_count, $page);
dd($paginator->toArray()); // view result

How to get limited data to paginating

$rowsPerPage = 50;
$num = 1;
$offsets = ($num - 1) * $rowsPerPage;
$data['trans'] = Transaction::withoutBranch()
->select('id', 'amount')
->where('payment_date', '>=', '2019-06-07')
->limit($offsets)
->get();
I'm using Laravel 4.2 to do the planning. I trying to get the data from data with 50 on the first page then the second page will get another new 50 data.
I did try the code but it still gets all of the data from the database.
Please try following:
$data['trans'] = Transaction::withoutBranch()
->select('id', 'amount')
->where('payment_date', '>=', '2019-06-07')
->limit($rowsPerPage)
->offset($offsets)
->get();

array_slice() expects parameter 1 to be array - Manually Creating A Paginator

I am trying to union 3 SQL query, so I can orderBy the duedates as newDate and make a manually create paginator.
But for some reason is throwing this exception:
array_slice() expects parameter 1 to be array, object given
my code:
public function index(Request $request)
{
$TMUDue = Equipments::Where('due1', '<>', '1990-01-01')
->select('*', 'due1 AS newDate');
$CalDue = Equipments::Where('due2', '<>', '1990-01-01')
->select('*', 'due2 AS newDate');
$SerDue = Equipments::Where('due3', '<>', '1990-01-01')
->select('*', 'due3 AS newDate');
$equipmentsDue = $SerDue->union($CalDue)->union($TMUDue)->orderBy('newDate')->get();
$page = Input::get('page', 1);
$paginate = 10;
$offSet = ($page * $paginate) - $paginate;
$itemsForCurrentPage = array_slice($equipmentsDue, $offSet, $paginate, true);
$equipmentsDue = new \Illuminate\Pagination\LengthAwarePaginator($itemsForCurrentPage, count($equipmentsDue), $paginate, $page);
return view('dashboard.index', compact('equipmentsDue'));
Am i doing something wrong?
Without manually creation of the database pagination the query works fine.
It clearly says that you need to pass array instead collection , so change it with ->toArray();
$equipmentsDue = $SerDue->union($CalDue)
->union($TMUDue)->orderBy($sort , $sort2)->get()->toArray();

Laravel merge 2 collection with paginator

I need to do paging with two different collection. Have priority listing. Money that is placed in the priority list.
Why do not you do a single query, you can say the priority list. Reason, when using filtering; city, county, category I choose. However, the site owner, having priority listing in the city, county, or even in one of the priority areas listed in the category you want.
So in a query keywords when using 3 criteria, other criteria will be enough in the first. Each page lists 20 records, if the primary listing of the 30 first listing, the second collection of records in the other I would like to be listed thereafter.
According to the criteria normally be with you now I share my code. Waiting for your suggestions. Thank you in advance.
I'm sorry for my bad english.
DB::table('UserAds')
->join('Ads', 'Ads.id', '=', 'UserAds.ad_id')
->join('Users', 'Users.id', '=', 'UserAds.user_id')
->join('AdCategory', 'AdCategory.ad_id', '=', 'Ads.id')
->join('AdEducationPrices', 'AdEducationPrices.ad_id', '=', 'Ads.id')
->join('City', 'City.id', '=', 'Ads.city_id')
->join('Town', 'Town.id', '=', 'Ads.town_id')
->leftJoin('AdMedias', 'AdMedias.ad_id', '=', 'Ads.id')
->select('Ads.*', 'AdEducationPrices.*', 'City.name as city_name', 'City.id as city_id',
'Town.name as town_name', 'Town.id as town_id', 'AdMedias.url')
->where('Ads.is_publish', 1)
->whereIn('AdCategory.category_id', $new_category_array)
->where('Ads.city_id', $city_id)
->where('Ads.town_id', $town_id)
->where('UserAds.is_purchased', true)
->where('UserAds.started_at', '<', date('Y-m-d H:i:s'))
->where('UserAds.finished_at', '>=', date('Y-m-d H:i:s'))
->where('AdMedias.type', 'picture')
->groupby('UserAds.ad_id')->orderby('finished_at', 'DESC')
->orderby('started_at', 'DESC')->paginate(20);
I used array_merge for 2 queries. And filtered records for paginator. Ty for reading my question. I hope to help someone
$new_collection = array_merge($Priority, $Ads);
$new_collection_count = count($new_collection);
$perPage = 20;
$currentPage = Input::get('page', 1) - 1;
$new_collection = array_slice($new_collection, $currentPage * $perPage, $perPage);
$Ads = Paginator::make($new_collection, $new_collection_count, $perPage);

ORM Kohana Pagination VS total records found

I'm having some problem with this script... works fine, but not for the total of records found on the query.
I have this function on my ORM called "mix". There are 3 tables: mixes, generos and mixes_generos.
I can search in the table "mixes" or on the table "generos", anyone.
public function search($query, $itemsperpage, $page)
{
if (empty($page))
$page = 1;
$offset = ($page - 1) * $itemsperpage;
$orm = $this->join('mixes_generos', 'LEFT')->on('mix.id', '=', 'mixes_generos.mix_id')
->join('generos', 'LEFT')->on('generos.id', '=', 'mixes_generos.genero_id')
->where_open()
->or_where('mix.nombre', 'LIKE', '%'.$query.'%')
->or_where('mix.descripcion', 'LIKE', '%'.$query.'%')
->or_where('generos.nombre', 'LIKE', '%'.$query.'%')
->where_close()
->and_where('mix.estatus', '=', 'active')
->order_by('mix.fecha_produccion', 'DESC')
->order_by('mix.track', 'DESC');
$this->_search_total = $orm->reset(FALSE)->count_all();
$orm = $orm->limit($itemsperpage)->offset($offset);
return $orm;
}
public function search_total()
{
return $this->_search_total;
}
Then, in my controller i have this:
$data = ORM::factory('mix')->search('pop music', 10, 1);
if I want to echo the total, i do this:
echo $data->search_total();
in my view I print the info:
foreach($data->find_all() as $mix)
echo $mix->nombre;
http://s10.postimage.org/cvnr28nop/records.jpg
in this link you will see the result, look closely the column "records_founds" vs the number of "records" that it founds, 3 vs 1??.
And that is the main problem... because when i print the data of the mixes shows 3 instead 1. I also add an group_by('mix.id') and nothing :(
What do you think?
Thank you

Categories