$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();
Related
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
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);
Laravel version 4.2
I'm having issues with this one problem I have.
I need to fill a quota of 5 results however I only have an array size of 3. I also need the rows to be selected randomly from the Array.
*edit: Fetching all results is not feasible
$store_id_array = array('111', '222' , '333');
$reviews = Review::whereIn('store_id', $store_id_array)
->take(5)
->get();
return Response::json($reviews);
This will return 5 results however only from the first item in the array.
How do I select randomly from the array items?
For Laravel below version 5:
orderBy(DB::raw('RAND()')) like this:
$reviews = Review::whereIn('store_id', $store_id_array)
->orderBy(DB::raw('RAND()'))
->take(5)
->get();
For Laravel 5.0 +
use inRandomOrder() method:
$reviews = Review::whereIn('store_id', $store_id_array)
->inRandomOrder()
->take(5)
->get();
public function index()
{
$categories = Category::latest()->get();
$protfolios = Protfolio::all();
$randompost = Post::approved()->publish()->take(4)->InRandomOrder()->get();
$allposts = Post::where('price', 0)->approved()->publish()->take(8)->get();
$posts = Post::latest()->where('price', '>=', 0)->approved()->publish()->take(8)->get();
$latestposts = Post::latest()->approved()->publish()->take(8)->get();
return view('welcome',compact('posts','randompost ','categories','allposts','latestposts','protfolios'));
}
I'm running this code on Laravel. I'm adding filters/ordering if I receive them and I'm altering the query before running it and then paginate the results.
$aDatas = DB::table('datas');
if (!empty($aLocations)) {
foreach ($aLocations as $oLocation) {
$aDatas->orWhere('pc', '=', $oLocation->pc);
}
}
if (!empty($oFilters->note)) {
$aDatas->where('note', '>=', $oFilters->note);
}
if (!empty($oFilters->nb_comments)) {
$aDatas->where('nb_comments', '>=', $oFilters->nb_comments);
}
if (!empty($oOrder->type)) {
$aDatas->orderBy($oOrder->type, $oOrder->sort);
}
// echo $aDatas->where('note', '>=', 5)->count() ????
It's working fine.
But I'd like to use these results to count several parts of it.
The last line shows what I tried to do, counting how many rows in these filtered results have a note >= 5. But doing this will actually filter my original data.
I thought about assigning $aDatas to another variable and then count on this, but I'll have many counts and that seems dirty.
Is there a sweet way to do this ?
Just save your datas an replace the last line with this:
$datas =$aDatas->where('note', '>=', 5)->get();
echo $datas->count();
//use datas here for more action.
For all of your requirement, you might want to resolve in making several queries because a single query will not be able to do that(based from what I know)
//this is to get your total of note greater than 5
$query = DB::table('datas');
$query->where('note', '>=', 5);
$data = $query->paginate(10);
$count = $data->getTotal();
to get your other data
If you are working with pagination, use getTotal() instead
$query = DB::table('datas');
$query->select(
DB::raw('COUNT(stars) AS count'),
DB::raw('starts'),
);
$query->where('notes', '>=', 5);
$query->groupBy('stars');
$data = $query->get();
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.