This has just started happening with no changes to our implementation.
When I query (it seems any item) the API is no longer returning data in the fields property.
PodioItem {#354 ▼
-__attributes: array:32 [▼
"item_id" => 1234567890
"external_id" => null
"title" => "INV-1234567890-Deposit"
"link" => "[redacted]"
"rights" => array:8 [▶]
"created_on" => "2019-09-10 22:28:37"
"app_item_id_formatted" => "1310"
"app_item_id" => 1310
"created_by" => PodioByLine {#362 ▶}
"created_via" => PodioVia {#363 ▶}
"initial_revision" => PodioItemRevision {#364 ▶}
"current_revision" => PodioItemRevision {#367 ▶}
"fields" => PodioVia {#370 ▼
-__attributes: []
-__belongs_to: array:2 [▶]
-__properties: array:5 [▶]
-__relationships: []
#__id_column: null
}
"like_count" => null
"is_liked" => false
"ratings" => array:1 [▶]
"user_ratings" => array:1 [▶]
"last_event_on" => "2019-09-10 23:17:06"
"participants" => []
"tags" => []
"refs" => []
"linked_account_id" => null
"subscribed" => false
"invite" => []
"app" => PodioApp {#371 ▶}
"ref" => PodioReference {#372 ▶}
"reminder" => PodioReminder {#373 ▶}
"recurrence" => PodioRecurrence {#374 ▶}
"linked_account_data" => PodioLinkedAccountData {#375 ▶}
"comments" => PodioLinkedAccountData {#376 ▶}
"revisions" => PodioVia {#392 ▶}
"files" => PodioVia {#401 ▶}
]
-__belongs_to: null
-__properties: array:38 [▶]
-__relationships: array:15 [▶]
#__id_column: "item_id"
}
You can see __attributes are empty for fields. Again, for every item I have tested and for several apps, it is the same.
As I said nothing has changed in our implementation - last week it was working fine.
Any ideas?
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a collection array of 'Products', as seen from the dump below.
Illuminate\Support\Collection {#918 ▼
#items: array:12 [▼
0 => App\Product {#934 ▶}
1 => App\Product {#943 ▶}
2 => App\Product {#959 ▶}
3 => App\Product {#968 ▶}
4 => App\Product {#984 ▶}
5 => App\Product {#993 ▶}
6 => App\Product {#1009 ▶}
7 => App\Product {#1018 ▶}
8 => App\Product {#1034 ▶}
9 => App\Product {#1043 ▶}
10 => App\Product {#1059 ▶}
11 => App\Product {#1068 ▶}
]
}
This array has ONLY two unique products;
App\Product {#934 ▼
...
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => 1
"slug" => "26027686555545"
"details" => App\DispatchStock {#900 ▼
...
#attributes: array:9 [▼
...
"quantity" => 3
"created_at" => "2020-05-04 15:56:07"
]
...
}
]
...
}
and
App\Product {#943 ▼
...
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => 2
"slug" => "26027687444410"
"details" => App\DispatchStock {#899 ▼
...
#attributes: array:9 [▼
...
"quantity" => 5
"created_at" => "2020-05-04 15:56:07"
]
...
}
]
...
}
duplicated 6 times each (with varying 'quantity's).
I want to loop through the collection array of 'Products', merge all the duplicates, and return an array of just the unique products.And have something like:
Illuminate\Support\Collection {#918 ▼
#items: array:2 [▼
0 => App\Product {#934 ▶}
1 => App\Product {#943 ▶}
]
}
While appending a new attribute 'combined_available_quantity' (the sum of all the 'quantity's of the Products) to the 'details' property of each of the unique Products.
For example, I want one of the final 'Product's to look like:
App\Product {#943 ▼
...
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => 2
"slug" => "26027687444410"
"details" => App\DispatchStock {#899 ▼
...
#attributes: array:9 [▼
...
"quantity" => 5
"created_at" => "2020-05-04 15:56:07"
"combined_available_quantity" => 27
]
...
}
]
...
}
I already started with the code below.
...
$combinedProducts = collect();
foreach ($products as $product) {
$combinedProducts = $combinedProducts->each(function($combinedProduct) use ($combinedProducts, $product) {
if ($combinedProduct->slug == $product->slug) {
$qty = $product->details->quantity + $combinedProduct->details->quantity;
$combinedProduct->details->setAttribute('combined_available_quantity', $qty);
} else {
$product->details->setAttribute('combined_available_quantity', $product->details->quantity);
$combinedProducts->push($product);
}
});
$combinedProducts = $combinedProducts->values();
}
But it's buggy, and doesn't give me required results.
I need a better approach. Thanks.
$collect->groupBy('slug')->map(function($item){
return $item->sum('quantity');
})
I've created a collection like this :
Collection {#651 ▼
#items: array:3 [▼
0 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#656 ▶}
"foods" => array:2 [▶]
]
1 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#661 ▶}
"foods" => array:2 [▶]
]
2 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#665 ▶}
"foods" => array:2 [▶]
]
]
}
I demand to achieve collection like this (with Laravel collection):
Collection {#651 ▼
#items: array:3 [▼
0 => array:3 [▼
"orderId" => "402457"
"orderCreated" => DateTime {#656 ▶}
"foods" => array:6 [▶]
]
]
}
Because orderId and orderCreated are all the same in arrays. I need to make single array that collect orderId and orderCreated with all foods.
Any suggestion?
You can take the data from the first one and just combine all of the food items. For instance like this:
$new = collect([
'orderId' => $old->first()->orderId,
'orderCreated' => $old->first()->orderCreated,
'foods' => $old->pluck('foods')->flatten(1),
]);
The exact implementation will depend on how you built your initial collection.
I am using an API from themoviedb.org, which is returning an array:
array:20 [▼
0 => TVShow {#186 ▼
-_data: array:13 [▼
"original_name" => "Star Trek: Discovery"
"id" => 67198
"name" => "Star Trek: Discovery"
"vote_count" => 48
"vote_average" => 6.3
"poster_path" => "/ihvG9dCEnVU3gmMUftTkRICNdJf.jpg"
"first_air_date" => "2017-09-24"
"popularity" => 75.562899
"genre_ids" => array:1 [▶]
"original_language" => "en"
"backdrop_path" => "/s3kVP6R3LbJvvoPnDQEcJNEH2d0.jpg"
"overview" => "Ten years before Kirk, Spock, and the Enterprise there was Discovery. Star Trek, one of the most iconic and influential global television franchises, returns 5 ▶"
"origin_country" => array:1 [▶]
]
}
1 => TVShow {#187 ▶}
2 => TVShow {#193 ▶}
3 => TVShow {#194 ▶}
4 => TVShow {#195 ▶}
5 => TVShow {#196 ▶}
]
I am trying access it like I normally would an array:
foreach($array as $item {
echo $item["original_name"];
}
But I am getting this error:
"Cannot use object of type TVShow as array"
Could someone please help? Thanks!
I solved my own question with lot's of handholding from #ArtisticPhoenix.
I am using TMDB PHP API wrapper. In order to access the "original_name" value, I needed to do this:
echo $item->getName();
As detailed in the wrapper documentation.
So I'm trying to make a function that searches a string in a whole array. But it give me anything... So my array looks like this :
array:1 [▼
"list" => array:2 [▼
"pagination" => array:5 [▶]
"entries" => array:11 [▼
0 => array:1 [▼
"entry" => array:8 [▼
"firstName" => "Doctor"
"lastName" => "Who"
"emailNotificationsEnabled" => true
"telephone" => "0123456789"
"company" => []
"id" => "DW"
"enabled" => true
"email" => "doctorwho#time.lord"
]
]
1 => array:1 [▶]
2 => array:1 [▶]
3 => array:1 [▶]
4 => array:1 [▶]
5 => array:1 [▶]
6 => array:1 [▶]
7 => array:1 [▶]
8 => array:1 [▶]
9 => array:1 [▶]
10 => array:1 [▶]
]
]
]
So for exemple at first I did $key = array_search("doctor", $users); but this gives me nothing. So I thought it was because I have a multidimensional array. So I reduced it to just one array (and I would search in the rest of the original array with a for loop), so now I'm working with this array that I got with $users['list']['entries'][0]
array:1 [▼
"entry" => array:8 [▼
"firstName" => "Doctor"
"lastName" => "Who"
"emailNotificationsEnabled" => true
"telephone" => "0123456789"
"company" => []
"id" => "DW"
"enabled" => true
"email" => "doctorwho#time.lord"
]
]
But $key = array_search("doctor", $users['list']['entries'][0]); still doesn't give me anything (but false).
Does anyone know where is my mistake ? Because I couldn't find a solution of my problem yet and it's been a pretty long time I'm on it... I'm still a beginner in php so maybe I've missed something obvious and i'm sorry if I did.
Thank you in advance !
you are searching for doctor,but value stored in your array is as Doctor. So either search for Doctor as
$key=array_search('Doctor', $users['list']['entries'][0]));
or for case in-sensetive search use
$key=array_search(strtolower('Doctor'), array_map('strtolower', $users['list']['entries'][0]));
Note: as your array is multidimensional, so use loop to search your value
suddenly i faced problem which I can't solve by myself. I wrote query which returns me some data:
$users = User::whereIn('users.status', array(2,3))
->leftJoin('firsts', 'firsts.user_id', '=', 'users.id')
->leftJoin('seconds', 'seconds.user_id', '=', 'users.id')
->leftJoin('thirds', 'thirds.user_id', '=', 'users.id')
->leftJoin('offers', 'offers.user_id', '=', 'users.id')
->where(function($query){
$query->where('users.status', '=', '2');
$query->where('firsts.status', '=', 'approved');
$query->where('seconds.status', '=', 'approved');
$query->where('offers.user_id', '=', 'approved');
})
->orWhere(function($query){
$query->where('users.status', '=', '3');
$query->where('users.stripe_active', '=', '1');
$query->where('firsts.status', '=', 'approved');
$query->where('seconds.status', '=', 'approved');
$query->where('thirds.status', '=', 'approved');
$query->where('offers.status', '=', 'approved');
})
->get();
I were expecting simple object as a return from query. At the moment I have one proper record in my users table, but this query returns me that object 6 times, and I can't understand why. Results I'm expect:
Collection {#353 ▼
#items: array:1 [▼
0 => User {#354 ▼
#dates: array:2 [▶]
#table: "users"
#fillable: array:3 [▶]
#hidden: array:2 [▶]
+admin: "eval.butkus#gmail.com"
+admin2: "Evaldas"
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:31 [▼
"id" => 1
"username" => "evaldas"
"password" => "$2y$10$ARTGHBTqIl/X6UsblylF5ugMPFlASwDKUEVbS5Nh3TjtycGG/NU7."
"email" => "eval.butkus#gmail.com"
"remember_token" => "A42ezMckkvjCLrpj4jxyHf2iSQWp3iAGfzGoTqMygDimFSzXNx6FW42qCOhP"
"name" => ""
"subscribe" => 0
"profile_picture" => "VNlQxOlCbXkCGEQ6ZdFS1pbK2tixhsmmhQPhLNLMBRKqDzXegWbqQiJOsplMADKg.jpg"
"about_you" => ""
"writing_service" => 0
"f_card" => 0
"logo" => ""
"website" => ""
"youtube" => ""
"status" => 3
"code" => ""
"active" => 1
"address" => ""
"phone" => ""
"news" => 0
"terms" => 1
"radius" => 0
"created_at" => "2015-07-13 10:26:47"
"updated_at" => "2015-07-15 10:25:30"
"stripe_active" => 1
"stripe_id" => "cus_6c0xE8OxR7fdXJ"
"stripe_subscription" => "sub_6c0x4RzEoBplCb"
"stripe_plan" => "small"
"last_four" => "4242"
"trial_ends_at" => null
"subscription_ends_at" => null
]
#original: array:31 [▶]
#relations: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
]
}
Results i get:
Collection {#364 ▼
#items: array:6 [▼
0 => User {#365 ▼
#dates: array:2 [▶]
#table: "users"
#fillable: array:3 [▶]
#hidden: array:2 [▶]
+admin: "eval.butkus#gmail.com"
+admin2: "Evaldas"
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:53 [▼
"id" => 2
"username" => "evaldas"
"password" => "$2y$10$ARTGHBTqIl/X6UsblylF5ugMPFlASwDKUEVbS5Nh3TjtycGG/NU7."
"email" => "sadsad"
"remember_token" => "A42ezMckkvjCLrpj4jxyHf2iSQWp3iAGfzGoTqMygDimFSzXNx6FW42qCOhP"
"name" => "sadsa"
"subscribe" => 0
"profile_picture" => "VNlQxOlCbXkCGEQ6ZdFS1pbK2tixhsmmhQPhLNLMBRKqDzXegWbqQiJOsplMADKg.jpg"
"about_you" => ""
"writing_service" => 0
"f_card" => 0
"logo" => ""
"website" => "das"
"youtube" => ""
"status" => "approved"
"code" => ""
"active" => 1
"address" => "asfas"
"phone" => "dasd"
"news" => 0
"terms" => 1
"radius" => 0
"created_at" => "2015-07-15 07:31:37"
"updated_at" => "2015-07-15 07:44:31"
"stripe_active" => 1
"stripe_id" => "cus_6c0xE8OxR7fdXJ"
"stripe_subscription" => "sub_6c0x4RzEoBplCb"
"stripe_plan" => "small"
"last_four" => "4242"
"trial_ends_at" => null
"subscription_ends_at" => null
"country" => "Highland"
"mobile" => "asd"
"descripsion" => "Ddescription"
"landline" => "asdas"
"company_name" => "das"
"facebook" => "dsad"
"twitter" => "sadsa"
"linkedln" => "dsadasd"
"company_number" => "sad"
"google" => "sadasd"
"vat" => "asdsad"
"experience" => ""
"privacy" => 0
"user_id" => 1
"post_code" => "AB115QN"
"latitude" => "57.14270109"
"longitude" => "-2.093014619"
"identify" => "grEa6JEaj6P0I7Zc3ZCq5CADojzApSJZ.jpg"
"bill" => "N8WKjRlSttsHNt1crrNdwxG7uqWEg5GB.jpg"
"comment" => "asdsad"
"image" => "7dmURWxbzO4pinqbek0yzdIKqEyZrjOD.jpg"
"title" => "new offer"
]
#original: array:53 [▶]
#relations: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
1 => User {#366 ▶}
2 => User {#367 ▶}
3 => User {#368 ▶}
4 => User {#369 ▶}
5 => User {#370 ▶}
]
}
Could any explain how did i lose all the relationships between tables and how can I avoid it? Since structure of result changed, I can't use loops, relationships and etc. Thanks!.
If im reading this right, you just want to
(delete join onto orders)...->groupBy('users.id')->get();
That will return you one user matching the conditions.
You can achieve this with 2 queries, first group by on users (don't join orders), then do a second query for the orders. What else to join would depend on which tables you are using to filter users by vs which tables you are using to add data to orders with.
Put data to add to orders in the second query, stuff your using to filter users in the first. You could also try a subquery but that isn't supported well in eloquent.
Also your whereIn is completely pointless, as you are already testing for where user status is 2 or 3.
Edited for a little more clarity,I was a little hasty. Originally thought that the poster wanted one row back - which wasn't the case.