Getting data from Laravel Collection - laravel

I realize this is a dumb question, but I can't find an answer for it. I am missing some understanding around how collections work
I am querying a google places API for information about a location
$response = GooglePlaces::textSearch('10 Downing Street', $optionnalParameters);
This works and gives me a response, which I can var_export:
Illuminate\Support\Collection::__set_state(array(
'items' =>
array (
'html_attributions' =>
array (
),
'results' =>
Illuminate\Support\Collection::__set_state(array(
'items' =>
array (
0 =>
array (
'formatted_address' => '10 Downing St, London SW1A 2AA, United Kingdom',
'geometry' =>
array (
'location' =>
array (
'lat' => 51.503363499999999,
'lng' => -0.12762480000000001,
),
'viewport' =>
array (
'northeast' =>
array (
'lat' => 51.503432400000001,
'lng' => -0.12551999999999999,
),
'southwest' =>
array (
'lat' => 51.503156800000014,
'lng' => -0.12832640000000001,
),
),
),
'icon' => 'https://maps.gstatic.com/mapfiles/place_api/icons/civic_building-71.png',
'id' => 'ffd85a3e543406e34703ee947c73ef54f5e167fe',
'name' => '10 Downing Street',
'photos' =>
array (
0 =>
array (
'height' => 2534,
'html_attributions' =>
array (
0 => 'Christopher Chan',
),
'photo_reference' => 'CoQBdwAAAHdqDNRUhalI7Ko_YXqckWM5M7I1IeD0xXOvjnxruS4BYCFnt99lCEy5xQJh7XtTvGTfZbKlnVhbxaJ_OloLxaPyoInqIpgRY-3LyB3Q70tDX3izeraFEM4Bw-ExmRzz6h18iMQlKb0DoDXnW26uO4RR-7YFjPNi6M0y5D7cmrl9EhAmI7GerM-TXpKD3BVVTtOWGhQZotS0ZNI2nK9G7jXxDEyvoQ5IxQ',
'width' => 3801,
),
),
'place_id' => 'ChIJRxzRQcUEdkgRGVaKyzmkgvg',
'rating' => 3.3999999999999999,
'reference' => 'CmRSAAAAh-Drhh9G_EW3azxZSikW_jR-ZjI2lhZTw6MfWqh9EiTaCEy4uW2okv_g6QfaKoupoeDu3DpfS6MjIvUvp6cc2uAoLlyH9NbkMrnQg9q3ED3R91OejmAScjRhe8G47kJ1EhD_oYVFakm4n6I27j-8iN6oGhQVH6rv1t6uGQBVVOqy1fCAB17JOg',
'types' =>
array (
0 => 'point_of_interest',
1 => 'establishment',
),
),
),
)),
'status' => 'OK',
),
))
I want to get the formatted_address element from the collection. it appears under the results element, that in turn has a collection under it with elements Items,0,formatted_address.
when i start trying to 'walk down' to the element i require I get index not found for Items
var_export($response['results']['items']);
so essentially, how am I supposed to interact with collections? i have tried google but most the links tell me about the extra functions that collections provide and not how to get data from it
any help greatly appreciated.

I think https://laravel.com/docs/5.3/collections#method-all explains what you are missing. You want to call
->all()
on your Collection to return the underlying array represented by the collection.
Than you can access fields in the returned array.

Related

Get patents data data only child has empty data in relationship?

I have a collection like the below. I need to fetch those users, whom does not uploaded documents. Now from the relationship, How can I fetch this?
[0] => Array
(
[id] => 2
[user_id] => 8
[referred_by] =>
........
[user_documents] =>
)
[1] => Array
(
[id] => 3
[user_id] => 9
[referred_by] =>
[reference_id] => NM1100008
.......
[user_documents] => Array
(
[id] => 1
[customer_id] => 3
[doc_type] => 1
[document] => N8xFORsPpkTayTQ9Ihyz0ly7QM62TJHxvHhSFQSN.png
[admin_comment] =>
[approve_status] => approve
[other_gov_id] =>
[doc_no] => 1
[created_at] => 2020-04-04 12:39:12
[updated_at] => 2020-04-06 13:57:01
[deleted_at] =>
)
)
I want to get rows only when user_documents is empty.. To get those users who does not uploaded documents to the system.
[user_documents] =>
Thanks in advance.
You can use laravel querying-relationship-absence
$users= User::doesntHave('user_documents')->get();
Depending on how you have setup your relations, something like this should work:
User::doesntHave('documents')->get();

Strange assertJSON behaviour

I want to test, whether an AJAX response contains the array I'm expecting.
So far so good, not really a great deal.
This is how my array should look like:
array (
'data' =>
array (
0 =>
array (
'key1' => 'value1',
'key2' => 'value2,
),
1 =>
array (
'key1' => 'value3',
'key2' => "value4",
),
),
)
When I run my test:
$request->assertJson([the array mentioned above]);
The array really looks like that but it fails anyway. Why?
because in reality it expects the array twice.
In the comparison window, I see that it expects this:
array (
'data' =>
array (
0 =>
array (
'key1' => 'value1',
'key2' => 'value2,
),
1 =>
array (
'key1' => 'value3',
'key2' => "value4",
),
),
0 =>
array (
'key1' => 'value1',
'key2' => 'value2,
),
1 =>
array (
'key1' => 'value3',
'key2' => "value4",
),
)
But got the array mentioned above (which would be what I expect too).
When I run $request->assertJSON([]); the test succeeds but this can't be the way it's supposed to work, is it?
This is not a real answer (in terms of solving the underlying problem), but as I'm supposing this to be a bug, I want to share a workaround with people who encounter this problem too:
It's pretty simple. Just store the json into a variable $array = $request->json() (assuming that the response is saved into the $request variable.
Then test the contained array.
$this->assertEquals(EXPECTED_DATA, ARRAY_TO_TEST).

How read pubdate field using willvincent/feeds plugin?

I use https://github.com/willvincent/feeds for reading rss and with all field I also need to get pubDate field.
iI did not find a method for it(I looked also in source app/library/AppRssImport.php file ) and I made some debugging, so methods
echo '<pre>$RssItem->data[\'child\']::'.print_r($RssItem->data['child'],true).'</pre>';
dd($RssItem->data['child']);
has next output:
$RssItem->data['child']::Array
(
[] => Array
(
[title] => Array
(
[0] => Array
(
[data] => Man died 'in agony' after stroke amid ambulance delays
[attribs] => Array
(
)
[xml_base] =>
[xml_base_explicit] =>
[xml_lang] =>
)
)
[description] => Array
(
[0] => Array
(
[data] => Michelle Lane has PTSD and flashbacks of her husband screaming in pain as they went to hospital by car.
[attribs] => Array
(
)
[xml_base] =>
[xml_base_explicit] =>
[xml_lang] =>
)
)
[link] => Array
(
[0] => Array
(
[data] => https://www.bbc.co.uk/news/uk-england-nottinghamshire-46795776
[attribs] => Array
(
)
[xml_base] =>
[xml_base_explicit] =>
[xml_lang] =>
)
)
[guid] => Array
(
[0] => Array
(
[data] => https://www.bbc.co.uk/news/uk-england-nottinghamshire-46795776
[attribs] => Array
(
[] => Array
(
[isPermaLink] => true
)
)
[xml_base] =>
[xml_base_explicit] =>
[xml_lang] =>
)
)
[pubDate] => Array
(
[0] => Array
(
[data] => Thu, 10 Jan 2019 10:02:22 GMT
[attribs] => Array
(
)
[xml_base] =>
[xml_base_explicit] =>
[xml_lang] =>
)
)
)
[http://search.yahoo.com/mrss/] => Array
(
[thumbnail] => Array
(
[0] => Array
(
[data] =>
[attribs] => Array
(
[] => Array
(
[width] => 1024
[height] => 576
[url] => http://c.files.bbci.co.uk/15CE0/production/_105121398_michelle-tony.jpg
)
)
[xml_base] =>
[xml_base_explicit] =>
[xml_lang] =>
)
)
)
)
and https://imgur.com/a/wTmd9cV
I tried to read value of pubDate field, but failed. This structure was rather strange for me...
Which is right decision ?
You can use the get_date() method to grab the value of pubDate?
https://github.com/willvincent/feeds this plugin is only a wrapper around SimplePie php extension. You can look into SimplePie documentation for all other available methods. http://simplepie.org/api/class-SimplePie_Item.html.
A usable code might be like this. I used laravel, so the first line of grabbing the feed might be different.
$feed = Feeds::make('https://www.example.com/feed/');
$items = $feed->get_items();
foreach( $items as $key => $item ) {
$title = $item->get_title();
$guid = $item->get_id();
$pub_date = $item->get_date();
}

ElasticSearch Aggregation Past _source

I am trying to get a list of all possible article groups. My Product array example is like so:
[8] => Array
(
[_index] => product_index
[_type] => product_51_DEU
[_id] => AV7mxnScT3P3M-G9u9aK
[_score] => 1
[_source] => Array
(
[artikelnummer] => G123456
[produktname] => My Cool Name Here
[artikeltext] => BLA
[produktgruppe] => Car Products
[anwendungsbezeichnung] => Wash Products
[lieferant] => Turtle
)
)
My PHP search parameter looks like this:
$mainMenuParams = [
'index' => 'product_index',
'type' => 'product_51_DEU',
'body' => [
'aggs' => [
'_SOURCE' => [
'terms' => [
'field' => '_source.produktgruppe'
]
]
]
]
];
$listProduktGroup = $GLOBALS["client"]->search($mainMenuParams);
I get an answer but there are no aggregations. I have tried many combinations, but none seem to work. Anyone have any idea where this is wrong? I want to see an aggregation with an output of all of the possible [produktgruppe]. There are 10 Groups in all, but it would be nice to see this in the results and then maybe even a count of all products in each group.
If I do the exact same query on "_types" I get accurate results.

ErrorException in mycontroller on line 137 Undefined offset: 2

I am getting this exception: Undefined offset: 2 when inserting a multidimensional array in the database. This is my loop at which the error is pointing at:
$order_details = [];
for($i= 0; $i < count($product); $i++){
$order_details[] = [
'order_id' => $orders->id,
'product_id' => $product['product_id'][$i],
'units'=>4,
'quantity' => $product['quantity'][$i],
'unit_price' => $product['price'][$i],
'product_discount_id'=>0,
'amount' => $product['amount'][$i],
];
}
When I echo my array seems to look fine:
Array ( [product_id] => Array ( [0] => 7 [1] => 1 ) [quantity] => Array ( [0] => 2 [1] => 1 ) [price] => Array ( [0] => 200.00 [1] => 700.00 ) [amount] => Array ( [0] => 400 [1] => 700 ) )
The undefined offset value alternates depending on how many items from the shopping cart are passed to the loop. For example if there are 3 items the error is set to Undefined offset:3
Its like I cannot find out the real mole. I have searched the web I have not managed any way out.
Any assistance Kindly. what May I doing wrong?
The errors you are getting are about not having an element with index of count($product).
You should just use:
for($i= 0; $i <count($product)-1; $i++)
This way you loop will be fine.
That's kind of an odd array structure. You'd have a much easier time working with the data if you rearranged it like this so you have an item per product:
$products = [
[
'product_id' => 7,
'quantity' => 2,
'price' => 200.00,
'amount' => 400
],
[
'product_id' => 1,
'quantity' => 1,
'price' => 700.00,
'amount' => 700
]
];
Then you can just foreach over the products.
The fix for your problem though is this for($i= 0; $i <= count($product); $i++)

Resources