Show page:2 directly in View page in CAKEPHP - cakephp-2.1

Is there any option in $this->Paginator->Settings() to paginate a table and store second or third page to the variable using
$view=$this->Paginate('Post');

You can specify the page with the following:
$this->Paginate->settings(array('page' => 2));
$posts = $this->Paginate->paginate('Post');
$posts now contains the records for page 2

Related

How to split the result of Laravel paginator

I am using the Laravel paginator on my site. I want to split the query result into 4 parts. Because my design has 2 rows of 3 items each and another 2 rows of 2 items each. So I can't use a single foreach loop for showing the items. In my controller, I have the below code.
$properties = Property::whereHas('createdBy', function ($query) {
$query->where('enabled','=',true);
})->orderBy('created_at', 'desc')->paginate(10);
And in my view, I am trying to split the result. I have tried $properties->take(3) and $properties->chunk(3). But is not working.
Below is the content of paginator.
Please help.

detach laravel paginate object and list items to separate veriables

hello, is that possible to detach Laravel pagination object (without items) to a another variable and its items to another variable?
eg:
I'm currently working on product list that project that using without pagination (just ->get() ) so I need to inject paginated items to that variable and send pagination Object data in separate variable.. hope you guys can understand.
the current way is:
$products = Products::get();
I just need send pagination items to that variable and pagination object in another variable.
just like this:
$products_paginate = Products::paginate(10);
$paginateObj = $products_paginate ->pagination();
$products = $products_paginate ->items();
items should not in pagination data object.

Return laravel query results in chunks

I have an Update model in my Laravel/Vue.js app, instead of retrieving and displaying all results at once in my component, I want to return them in chunks of fives and place a NEXT FIVE UPDATES link in my component to display the next 5 update records, much like in pagination. I tried this:
$update = Update::limit(5)->get();
This does not achieve my desired result. Is there a Laravel method i can use in my laravel backend to send the results in chunks of 5 to my Vue.Js component and then make my NEXT FIVE UPDATES link display the next 5 records.
If you're able to send some kind of page or offset value to the backend then you could use laravel's skip and take methods: https://laravel.com/docs/5.7/queries#ordering-grouping-limit-and-offset
$skip = $request->page; //assuming this variable exists...
$limit = 5;
$update = Update::skip($skip * $limit)->take($limit)->get();

Add row manually to eloquent result in Laravel 4.2

I am using Laravel 4.2 and i fetch all locations with this code:
$locations = Location::all();
This Locations are displayed in a select box afterwards. How can i add an additional row to the results in order to show an empty first option in the select box.
The options then should be:
choose a location
location 1
location 2
...
I just want to add an additional item to the result in $locations.
Thanks in advance
You can use:
{!! Form::select('location', ['' => 'Select your location'] + $locations, null , ['class' => 'form-control']) !!}
to update the view. In Laravel 5 there are attribute accessors to append an extra field with your eloquent collection.
Few other ways to do this are:
$locations[null] = 'choose a location';
Form::select('location', $locations);
Form::select('location',[null=>'Please Select'] + $locations);
Another way is to loop through the result and update it. Use json_decode() or 'toArray()` to convert your result into array format.
Otherwise you have to store choose a location as the first row value in your locations table(I know that is inappropriate for the requirement).
You should look at the put method for collections:
https://laravel.com/docs/5.6/collections#method-put
This method is not available in 4.2. You should make a custom Collection class and use it in your model by overwriting the newCollection method. https://laravel.com/docs/4.2/eloquent#collections

orderBy-querybuilder and pagination in laravel 5

I'm trying to make an orderBy in my query and a pagination for the items from a gallery, but instead of sort the complete query, laravel order only page-by-page content, so if I order desc it only works 1 page, when I go to the next page I can find values really different (ordered, but without relationship with the first page).
Here is my query:
$product = DB::table("product")
->leftJoin("provider","product.provider_id","=","provider.id")
->orderBy("daily_sales","DESC")
->select('product.*','provider.comercial_name')
->paginate(6);
You can try:
$product = DB::table("product")
->leftJoin("provider","product.provider_id","=","provider.id")
->select('product.*','provider.comercial_name')
->orderBy("daily_sales","DESC")
->paginate(6);

Resources