Using dd() function in Laravel - 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.

Related

How to get the State or Province using Geocoder Php

I'm wanting to reverse geocode coordinate to get the address using Geocoder-php. I'm able to get the Address Collection using $geo = app('geocoder')->reverse($lat, $lng)->get().
According to these docs found on github for Geocoder-php you can retrieve the properties form the collection, such as street name by using $geo->getStreetName(), the city by using $geo->getCity(), the country by using $geo->getCountry(), etc, but there are no methods for returning the State or Province.
How do I grab the state or province from the collection?
I can see the province when I use $geo->getAdminLevels()->get(). Which returns
AdminLevelCollection {#900 ▼
-adminLevels: array:2 [▼
1 => AdminLevel {#901 ▼
-level: 1
-name: "Saskatchewan"
-code: "SK"
}
2 => AdminLevel {#902 ▼
-level: 2
-name: "Division No. 16"
-code: "Division No. 16"
}
]
}
But I am unable to grab the province (Saskatchewan).
(I am using Laravel Geocoder for a Laravel project.)
EDIT:
$geo->getAdminLevels()->get(1) give me this
AdminLevel {#690 ▼
-level: 1
-name: "Saskatchewan"
-code: "SK"
}
But $geo->getAdminLevels()->get(1)->name; give me this error
Undefined property: Geocoder\Provider\GoogleMaps\Model\GoogleAddress::$getAdminLevels
The solution to this is to call it this way;
$geo->getAdminLevels()->get(1)->getName();
get() in this context takes a value of the index you wish to return.
You might want to check that the getAdminLevels() and get(1) return values if they may sometimes return nulls. You can do this by implementing something like !empty($geo->getAdminLevels())
You can do
$geo->getAdminLevels()->get()->adminLevels[0]->name or $geo->getAdminLevels()->get()->adminLevels[1]->name

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

Paginate json array in laravel

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.

Laravel 5 Blade - where condition in count

withing my view I have access to a collection of FoodTypes. I have cast it to an array to make it easier
to display
array:2 [▼
0 => array:7 [▼
"id" => 1
"name" => "Frozen"
"value" => "NOC M1"
]
1 => array:7 [▼
"id" => 2
"name" => "Fresh"
"value" => "NOC A1"
]
]
Initially, I had a Model for each individual FoodType e.g. Frozen and Fresh Models, but I soon discovered that
their tables/data were identical, so it seemed wrong to do this. Now I have a generic Model, with the name defining
the type of food.
Anyways, when I had a Model for each type, it was simple to do a count on the number of particular types. Now however,
I find myself doing something like this
count($data->foodTypes)
The problem is, I need to display the number for each individual type. The above will return 2 because it does not take
the name into consideration. So in essence, I need to do something like the following
count($data->foodTypes->where('name', '=', 'Frozen'))
Would something like this be possible?
Thanks
Yes you can use where.
The method signature from the base collection of eloquent collection:
public function where($key, $value, $strict = true);
That won't go to database again too.
$foods = \App\Foods::all();
$foods->where("food_type_id", 1)->count();

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.

Resources