How to search value in collection and merge model in laravel - laravel

I have 2 arrays and each array has deeply nested array below is the array
$invoices = $request->user()->invoices();
0 => array:74 [▼
"id" => "in_1MPwwCGBXoaOj5Wmk7lPY9tJ
"lines" => array:5 [▼
"object" => "list"
"data" => array:2 [▼
0 => array:24 [▶]
1 => array:24 [▶]
]
"has_more" => false
"total_count" => 2
"url" => "/v1/invoices/"
]
1 => array:74 [▼
"id" => "in_1MPwwCGBXoaO22vWmk7lPY9tJ
"lines" => array:5 [▼
"object" => "list"
"data" => array:2 [▼
0 => array:24 [▶]
1 => array:24 [▶]
]
"has_more" => false
"total_count" => 2
"url" => "/v1/invoices/"
]
i am trying to get the id from the deep nested array like this
$productnames = $invoices->first()->lines->first()->id;
and then search for the id in the modal like this
$plans = Plan::where('stripe_id', $productnames)->first();
it only display one id, and not all the id from the array,
also, when i merge both the collection it shows result like the below
$invoicescollection = new Collection($invoices);
$planscollection = new Collection($plans);
$products = $invoicescollection->merge($plans)->toArray();
array:10 [▼ // app/Http/Controllers/Billing/BillingController.php:26
0 => array:74 [▶]
1 => array:74 [▶]
"id" => 5
"title" => "All"
"slug" => "all"
"stripe_id" => "price_1MO55aOGBXoaOjvWmuQCJ14b3"
"active" => "0"
"created_at" => null
"updated_at" => null
]
i wanted to show the merge data in each array, not like the above.
thank you for the help.

Related

Laravel: Receive data from ChargeBee

I managed to create a Customer in ChargeBee but I don't know how to access each of its values.
To receive the information I use the following code, it works perfect for me ...
ChargeBee_Environment::configure("mysite-test","test_afhjsfslfsfslk438493sshgeh");
$result = ChargeBee_Customer::retrieve("16HHGDGJslfksds");
$customer = $result->customer();
$card = $result->card();
By doing a dd($result) I get the following:
ChargeBee_Result {#356 ▼
-_response: array:1 [▼
"customer" => array:22 [▼
"id" => "16HHGDGJslfksds"
"first_name" => "User"
"last_name" => "Test"
"email" => "user#mail.com"
"phone" => "555667788"
"auto_collection" => "on"
"net_term_days" => 0
"allow_direct_debit" => false
"created_at" => 1603420880
"taxability" => "taxable"
"updated_at" => 1603420880
"pii_cleared" => "active"
"resource_version" => 1603420880216
"deleted" => false
"object" => "customer"
"billing_address" => array:6 [▶]
"card_status" => "no_card"
"promotional_credits" => 0
"refundable_credits" => 0
"excess_payments" => 0
"unbilled_charges" => 0
"preferred_currency_code" => "USD"
]
]
-_responseObj: array:1 [▼
"customer" => ChargeBee_Customer {#358 ▶}
]
}
How can I get for example the id of this Customer??? Which in this case is 16HHGDGJslfksds
You can use this line:
$customerId = $result->customer()->id

Laravel Collection - Returning a total with sum() on nested arrays

I am trying to return a total using sum() by fetching the "duration" from each user within a $collection
The structure is
USER > CONTRACT_RESULTS > DAYS > DATE > ACTIVITIES > ACTIVITY
I want to query the "type" of activity and pull back the duration that is associated with that activity.
I had it working when it was much less nested using:
$collection->whereIn('activity',['off'])->sum('duration');
But I'm at a loss now since they are so nested and the names of the arrays are dynamic
Here is an example of the structure for 1 user in the $collection:
array:13 [▼
"user_id" => 52
"contract_results" => array:2 [▼
"2019-10-21 - 2019-10-31" => array:10 [▼
"hours_a_week" => "20"
"days" => array:1 [▼
"2019-10-21" => array:2 [▼
"bank_holiday" => "no"
"activities" => array:2 [▼
"10:00 - 22:00" => array:5 [▼
"type" => "driving"
"from" => "10:00"
"to" => "22:00"
"duration" => 720
"night_minutes" => 60
]
"22:00 - 00:00" => array:5 [▼
"type" => "driving"
"from" => "22:00"
"to" => "00:00"
"duration" => 120
"night_minutes" => 120
]
]
]
]
]
"2019-11-01 - 2019-11-10" => array:10 [▼
"hours_a_week" => "10"
"days" => array:1 [▼
"2019-11-01" => array:2 [▼
"bank_holiday" => "yes"
"activities" => array:1 [▼
"10:00 - 22:00" => array:5 [▼
"type" => "availability"
"from" => "10:00"
"to" => "22:00"
"duration" => 720
"night_minutes" => 60
]
]
]
]
]
]
]
I'm new to this so, thank you very much for your input!
Based on your unclear information, this is all that I can do:
$durationTotal = collect($data->get('contract_results'))
->transform(function ($contractResult) {
return array_merge((array) $contractResult, [
'days' => collect(data_get($contractResult, 'days'))
->transform(function ($day) {
return array_merge((array) $day, [
'activities' => collect(data_get($day, 'activities'))
->whereIn('type', ['driving'])
->toArray()
]);
})
->toArray()
]);
})
->sum(function ($item) {
return array_sum(data_get($item, 'days.*.activities.*.duration', []));
});

values are not being stored correctly in the array $items

I want to get the quantity of each registration type with the code below:
$registrationTypeDetails = Registration::with('participants:id,registration_type_id,registration_id')->find($regID);
$type_counts = [];
foreach ($registrationTypeDetails->participants as $p) {
$name = $p->registration_type->name;
if (!isset($type_counts[$name])) {
$type_counts[$name] = 0;
}
$type_counts[$name]++;
}
The dd($type_counts), if the conference has 2 registration types available (general and plus) and the user is doing a registration with 2 participants in the registration type "general" and 0 participants in the registration type "plus" shows:
array:2 [▼
"general" => 2
]
Then I need to make a post request to an API where is necessary to send in the request body the quantity of each registration type, in this case there is only 1 registration type "general" and the value for quantity should be "2".
So I have this code below the above code to create the array:
foreach ($registrationTypeDetails->participants as $registrationType) {
$items['invoice']['items'][] = [
'name' => $registrationType->registration_type->name,
'unit_price' => $registrationType->registration_type->price,
'quantity' => $type_counts[$registrationType->registration_type->name],
];
}
$create = $client->request('POST', 'https://...', [
'query' => ['api_key' => '...'], 'json' => $items,
]);
But then the output array is:
array:1 [▼
"invoice" => array:4 [▼
"client" => array:7 [▶]
"items" => array:2 [▼
0 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
1 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
]
]
]
Instead of only 1 item:
array:1 [▼
"invoice" => array:4 [▼
"client" => array:7 [▶]
"items" => array:2 [▼
0 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
]
]
]
$items shows:
array:1 [▼
"invoice" => array:4 [▼
"items" => array:2 [▼
1 => array:5 [▼
"name" => "general"
"unit_price" => 10
"quantity" => 2
]
2 => array:5 [▼
"name" => "plus"
"unit_price" => 0
"quantity" => 2
]
]
]
]
You would need to key the results of the items according to the registration type name:
foreach ($registrationTypeDetails->participants as $registrationType) {
$items['invoice']['items'][$registrationType->registration_type->name] = [
'name' => $registrationType->registration_type->name,
'unit_price' => $registrationType->registration_type->price,
'quantity' => $type_counts[$registrationType->registration_type->name],
];
}
Better is to use the id of the registration_type if there is one:
foreach ($registrationTypeDetails->participants as $registrationType) {
$items['invoice']['items'][$registrationType->registration_type->id] = [
'name' => $registrationType->registration_type->name,
'unit_price' => $registrationType->registration_type->price,
'quantity' => $type_counts[$registrationType->registration_type->name],
];
}
Explanation
As you loop over the participants linked to a registration type multiple participants could have the same registration type. So in your case you need to group the results according to registration type. Just adding to items[] appends to the items array. You want to group the results by registration type.
Fixing the 422 validation error
Your items is a valid array, but because it is translated to json the 1 and 1 would be keyed as an object. To force the key adjustment you can do the following:
// It is hacky but it re-keys the items array numerically.
$items['invoice']['items'] = array_values($list['invoice']['items']);

How to groupby() and sortby() a Laravel collection?

This is the structure of my collection
0 => array:13 [▼
"id" => 173
"campaign_id" => "STM1234"
"product_id" => "STM1234"
"group" => "24"
"version" => "1"
"created_at" => "2017-08-04 14:02:33"
"updated_at" => "2017-08-04 14:02:33"
"groups" => array:5 [▼
"id" => 24
"campaign_id" => "STM1234"
"group_name" => "default"
"updated_at" => null
I am trying to group and order this collection.
$groups = $products->groupBy('groups.group_name')->sortBy('groups.id');
The grouping works however the ordering does not work
array:2 [▼
"group2" => array:3 [▶]
"default" => array:8 [▶]
]
Default has an id of zero so should be first what do I need to do to get the group with the id of 0 first on the list?

laravel result array of array

I have this eloquent query
$data= \App\Logs::select('id', 'created_at')
->get()
->groupBy(function($val) {
return Carbon::parse($val->created_at)->format('d-M-');
})->toArray();
and this retun something like that
array:2 [▼
"06-Jul-" => array:1 [▼
0 => array:2 [▼
"id" => 1
"created_at" => "2017-07-06 13:21:15"
]
]
"07-Jul-" => array:3 [▼
0 => array:2 [▼
"id" => 2
"created_at" => "2017-07-07 13:43:23"
]
1 => array:2 [▼
"id" => 3
"created_at" => "2017-07-07 14:18:36"
]
2 => array:2 [▼
"id" => 4
"created_at" => "2017-07-07 14:18:41"
]
]
]
and i want return just the count
"06-Jul-" => "1"
"07-Jul-" => "3"
how i can do that?
You can use/chain map/transform after your groupBy for example:
$data = \App\Logs::select('id', 'created_at')
->get()
->groupBy(function($val) {
return Carbon::parse($val->created_at)->format('d-M-');
})
->transform(function ($collection, $key) {
return $collection->count();
});

Resources