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
Related
I am trying to use offset and limit on Laravel eloquent.
$start_from = 0;
$limit = 2;
$invoices = Invoice::where('calculation_complete',0)
->offset($start_from)
->limit($limit)
->get();
Instead of giving me 2 record it is giving me all the records. Where it is going wrong I am using it on other places there it is working fine.
Laravel has own function skip for offset and take for limit.
Try this:
$start_from = 0;
$limit = 2;
$invoices = Invoice::where('calculation_complete',0)
->orderBy('id','DESC')
->skip($start_from)
->take($limit)
->get();
Or, you can use paginate:
$invoices = Invoice::where('calculation_complete',0)
->orderBy('updated_at', 'desc')->paginate($limit);
$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();
I'm using laravel-page-view-counter to count visits of my products and it's working just fine, what i need to do is to get list of top 10 products by their visits (get 10 products which has largest number of visits).
Here is what I have:
$visits = Product::join('page-visits', function ($join) {
$join->on('products.id', '=', 'page-visits.visitable_id');
})->get();
$sorted = $visits->sortBy(function ($product, $key) {
return count($product['visits']);
});
But It return from lowest product visits to highest one (it shows product with 0 visit till product with 100 visits) I need reverse method of that to show (product with 100 visits first).
You can do it easily with query builder and some raw queries like this:
$visits = DB::table('products')
->join('page-visits','products.id','=','page-visits.visitable_id')
->select(DB::raw('count(visitable_id) as count'),'products.*')
->groupBy('id')
->orderBy('count','desc')
->take(10)
->get();
I hope you will understand.
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.
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);