Paginate json array in laravel - laravel-5

I'm currently using Laravel 5.6 and I have a result from an api. I'm trying to paginate the results and I'm not able to do it.
My json result is like this:
{#922 ▼
+"orders": array:50 [▼
0 => {#220 ▶}
1 => {#234 ▶}
2 => {#250 ▶}
3 => {#263 ▶}
4 => {#274 ▶}
5 => {#297 ▶}
6 => {#310 ▶}
7 => {#322 ▶}
8 => {#338 ▶}
9 => {#351 ▶}
]
}
I then get orders and put into a variable like so: $orders = $call->orders;
I have attempted this:
$orders = $shopifyData->paginate(20)->toArray();
I get this error:
Call to a member function paginate() on array
I have also attempted this: $orders::Paginate(20);
Also doesn't work.
I think I'm missing the first step to how to do this but honestly I'm lost. I'm not really sure how to go about it. Googling has several examples on how to do it when you are using an Eloquent model to retrieve the data from a database. It also shows how to do it when you query a database but nothing. The laravel documentation found under 5.6 pagination:
Paginating Query Builder Results
Paginating Eloquent Results
Manually Creating A Paginator
is very limited and only says to use paginator or lengthAwarePginator and to use array_slice.
Can someone point me in the right direction of how to achieve this?

I assume you are trying to paginate a plain-old PHP array in Laravel way.
The paginate function is only for query builders, so it doesn't work for arrays.
To do what you want, you first need to know at which page the user viewing. Usually, the current page is passed via the page GET parameter, but Laravel's LengthAwarePaginator offers functionality to extract that. You will need a use for this to work.
use Illuminate\Pagination\LengthAwarePaginator; // don't forget to add this
....
$current_page = LengthAwarePaginator::resolveCurrentPage();
Then you need to slice the array containing the result. Set the $perPage variable accordingly. Then you either need:
to turn your array into a Collection and slice it
$orders = $call->orders;
$orders_collection = new Collection($orders); // needs a use statement
$orders_collection = collect($orders); // alternative. You can use helper function
$current_page_orders = $orders_collection->slice(($current_page - 1) * $perPage, $perPage)->all(); // slice($offset, $number_of_item)
or to use array_slice directly for your array.
$orders = $call->orders;
$current_page_orders = array_slice($orders, ($current_page - 1) * $perPage, $perPage);
$current_page_orders is still a (sliced) Collection or array, so let's create a paginator instance.
$orders_to_show = new LengthAwarePaginator($current_page_orders, count($orders_collection), $perPage);
$orders_to_show is now the paginated result you want. You should be able to use it just like you handle paginated results from your database.

Related

Get result as object not array in laravel

I have this simple sql query
$orders=DB::table('carts')
-
->join('prod_suppliers','carts.prod_supplier_id','=','prod_suppliers.id')
->join('tickets','carts.ticket_id','=','tickets.id')
->distinct()
->select(
'suppliers.id as supplier_id',
'suppliers.supplier',
'suppliers.house_no_street_no',
'barangays.brgy_name',
'suppliers.city_id',
'tickets.id',
'tickets.ticket_no',
)
->get();
dd($orders);
i want my result to be an object but im getting result as an array
Collection {#525 ▼
#items: array:2 [▶]
}
what is the best way i can do to solve this problem?
It is a collection of Order objects. The dd() shows array, but those are objects inside that array. If you loop on the $orders variable, you will have ability to use object notation to get the fields you need.
For example:
foreach($objects as $object){
echo ($object->id); // or whatever field you need from the $order object
}
This is actually where Laravel really shines though - it has a fantastic model system. Using a model to pull the data through a simple Eloquent query, along with any relationships you need, might make your like far easier.

Using dd() function in Laravel

While using dd() function in Laravel, if my output looks like this,
Collection {#194 ▼
#items: array:3 [▼
0 => Post {#195 ▶}
1 => Post {#196 ▶}
2 => Post {#197 ▶}
]
}
what are the meanings of the codes, such as #194, #195, etc? Are they helpful in any way?
According to VarDumper documentation - that's what dd() uses behind the hood:
#14 is the internal object handle. It allows comparing two consecutive dumps of the same object.
Depending on whether the item being dumped is an object or PHP resource, you'll see # for objects and # for resources in there. The number you're seeing after # is the ID assigned by the VarDumper to that object. It's displayed so that you can easily identify dumps for the same object, if you dump it multiple times.

Laravel Custom Collections

I'm trying to understand something. Consider the following:
$collection = collect(['example1' => 'test1', 'example2' => 'test2']);
When I do the following, I end up with this result:
Collection {#867 ▼
#items: array:2 [▼
"example1" => test1
"example2" => test2
]
}
What I want to be able to do is this:
echo $collection->example1 // Should display test1
But instead it says "Trying to get property of non object".
So, I have two questions:
Can somebody explain the above behaviour?
Can somebody help with a solution so I can do $collection->example1?
Collections are objects containing an array. If you look back at what you posted you can see that you have an object of class Collection that contains an item which is an array. You can access the array items in normal array syntax or using the object's getter.
$product->get('subscription'); //object oriented way
$product['subscription']; //access as an array item

Laravel 5 - looping over a Collection

can't seem to figure this out. I have the following code
dd($this->project->dsReportingDoc->reportingDocUpload);
if(!empty($this->project->dsReportingDoc->reportingDocUpload)) {
dd("TEST");
foreach($this->project->dsReportingDoc->reportingDocUpload as $key){
}
}
Now the first dd prints out something like the following
Collection {#274 ▼
#items: array:2 [▼
0 => ReportingDocUpload {#275 ▶}
1 => ReportingDocUpload {#276 ▶}
]
}
So, there are two items in the Collection. However, the second dd never seems to get executed, so it must never make it into the if statement.
If anything is in the collection, I need to loop them and get a parameter. So I need to see if the item exists first.
Why would my if statement be failing here when it is not empty?
Thanks
The dd() debug function stops execution of the current request. So you can only call it once and get output - see here.
This is the reason your if condition and foreach aren't executing.
Try this
if($this->project->dsReportingDoc->reportingDocUpload) {
dd("TEST");
foreach($this->project->dsReportingDoc->reportingDocUpload as $key){
}
}
What you can do is, assign the
$this->project->dsReportingDoc->reportingDocUpload
to a variable so you don't have to rewrite it every where.

Laravel Update only 1 row?

Is there a way to update only 1 row in Laravel 5? I want to set the value of selected to true from only 1 row, ordered by id:
Ill tried:
DB::table('user_tabs')->where('user_id', '=', $user_id)->orderBy('id', 'desc')->take(1)->update(array('selected' => true));
And:
DB::table('user_tabs')->where('user_id', '=', $user_id)->orderBy('id', 'desc')->first()->update(array('selected' => true));
but it is not working. Any ideas?
Try this:
DB::table('user_tabs')->where('user_id', $user_id)->update(['selected' => true]);
You were trying to update the record after selecting it, which confused the Query Builder.
Also, have a look at Eloquent models. They make database interactions a little nicer.

Resources