laravel result array of array - laravel

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();
});

Related

How to search value in collection and merge model in 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.

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

How to get getClientOriginalName() from Image of array on Laravel?

I Have An Array from request Like This.... I have order_details table, and i want to update column category, remark and foto data where have orderid and skuid that i get from request,...
array:7 [▼
"orderid" => "191121120129KUR"
"_token" => "1ov7aCP8JzzokWNfMpwntTH0Dv084HqwIqMUfLd1"
"orderdet" => array:5 [▼
0 => array:4 [▼
"skuid" => "10006"
"category" => array:2 [▼
0 => "1"
1 => "2"
]
"remark" => "remarks 1"
"foto" => array:2 [▼
0 => Illuminate\Http\UploadedFile {#348 ▶}
1 => Illuminate\Http\UploadedFile {#349 ▶}
]
]
1 => array:4 [▼
"skuid" => "10007"
"category" => array:1 [▼
0 => "1"
]
"remark" => "remarks 2"
"foto" => array:4 [▼
0 => Illuminate\Http\UploadedFile {#350 ▶}
1 => Illuminate\Http\UploadedFile {#351 ▶}
2 => Illuminate\Http\UploadedFile {#352 ▶}
3 => Illuminate\Http\UploadedFile {#353 ▶}
]
]
2 => array:2 [▼
"skuid" => "10008"
"remark" => null
]
3 => array:2 [▼
"skuid" => "10138"
"remark" => null
]
4 => array:2 [▼
"skuid" => "10078"
"remark" => null
]
]
"actioncomplaint" => "1"
"salahsiapa" => "3"
"remarkcust" => null
"remarkfresh" => "Dani"
]
I want to store name of image using getClientOriginalName(),...
How to get that name of images , this is my controller
foreach($request->orderdet as $row)
{
$data = OrderDetail::where('data_order_id', $request->orderid)->where('skuid', $row['skuid'])->first();
if(isset($row['category'])) {
$data->remarkscomplaint = $row['remark'];
$data->categorycomplaint = implode(',', $row['category']);
$data->fotocomplaint = $row['foto'];
$data->save();
}
}
Try something like:
$fotos = $request->only(['foto']);
$fotoNames = []
foreach($fotos as $foto) {
$fotoNames[] = $foto->getClientOriginalName();
}
$implodedFotos = implode(',' $fotoNames);
then
foreach($request->orderdet as $row) {
$data = OrderDetail::where('data_order_id', $request->orderid)->where('skuid', $row['skuid'])->first();
if(isset($row['category'])) {
dd($row['foto']); //Array of Image
$data->remarkscomplaint = $row['remark'];
$data->categorycomplaint = implode(',', $row['category']);
$data->fotocomplaint = $implodedFotos; // <- set your attribute with the variable we create with the names
$data->save();
}
}
I guess you could also do something like
$fotos = collect($request->orderset['foto'])
->map(function ($foto) {
return $foto->getClientOriginalName();
})
->implode(',');
$fotos->all(); // access the result
and then in order to save your images this will help you.
$foto->storeAs('path', 'filename');

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', []));
});

How to combine two collections by matching keys and their values

So, I've been bashing my head against this for a while and I cant get it to just do what I need it to.
I want to be able to store a set of valuse in our database as Json, but no matter what I cant get Laravel or format the object quite right first.
I've got three queries, One gets users usernames and their First name, as an object. Another two use that object to get a count of each users sales, and quotes, from our databsae with count().
Now all I need is the objects to be combined.
array:1 [
"" => array:3 [
0 => array:7 [
0 => array:2 [
"CallCentreID" => "sdickerson"
"FirstName" => "Sarah"
]
1 => array:2 [
"CallCentreID" => "wjones"
"FirstName" => "Wendy"
]
2 => array:2 [
"CallCentreID" => "aknox"
"FirstName" => "Alex"
]
3 => array:2 [
"CallCentreID" => "mking"
"FirstName" => "Melissa"
]
4 => array:2 [
"CallCentreID" => "nrowecc"
"FirstName" => "Neil"
]
5 => array:2 [
"CallCentreID" => "ejones"
"FirstName" => "Emma"
]
6 => array:2 [
"CallCentreID" => "spurnell2"
"FirstName" => "Simon"
]
]
1 => array:5 [
0 => array:2 [
"CallCentreID" => "aknox"
"Sales" => 1169
]
1 => array:2 [
"CallCentreID" => "ejones"
"Sales" => 401
]
2 => array:2 [
"CallCentreID" => "mking"
"Sales" => 767
]
3 => array:2 [
"CallCentreID" => "sdickerson"
"Sales" => 1067
]
4 => array:2 [
"CallCentreID" => "wjones"
"Sales" => 716
]
]
2 => array:6 [
0 => array:2 [
"CallCentreID" => "aknox"
"Quotes" => 3587
]
1 => array:2 [
"CallCentreID" => "ejones"
"Quotes" => 400
]
2 => array:2 [
"CallCentreID" => "mking"
"Quotes" => 6975
]
3 => array:2 [
"CallCentreID" => "nrowecc"
"Quotes" => 3
]
4 => array:2 [
"CallCentreID" => "sdickerson"
"Quotes" => 2686
]
5 => array:2 [
"CallCentreID" => "wjones"
"Quotes" => 2734
]
]
]
]
The above is as close as I can get with the code I have which is;
$Users = User::where('Accesslevel', 'laravelcallcentre')->select('EmailAddress as CallCentreID', 'FirstName')->get();
$Sales = Order::groupBy('CallCentreID')
// ->whereDate('OrderDate', '=', date('2018-06-21'))
->whereIn('CallCentreID', $Users->pluck('CallCentreID'))
->where('Status', 'BOOKING')
->selectRaw('CallCentreID, count( * ) AS Sales')
->get();
$Quotes = Order::groupBy('CallCentreID')
// ->whereDate('OrderDate', '=', date('2018-06-21'))
->whereIn('CallCentreID', $Users->pluck('CallCentreID'))
->where('Status', 'QUOTE')
->selectRaw('CallCentreID, count( * ) AS Quotes')
->get();
$collection = collect([$Users, $Sales, $Quotes]);
$CCData = $collection->groupBy('CallCentreID');
Anyone got any better Ideas? I feel like im doing this completley wrong. I had this written out as a foreach loop but that ended up being four queries for every user, whihc stacked up fast.
$sales = $sales->keyBy('CallCentreID');
$quotes = $quotes->keyBy('CallCentreID');
$users = $users->map(function($user) use($sales,$quotes) {
$user->sales = isset($sales[$user['CallCentreID']]['sales']) ? $sales[$user['CallCentreID']]['sales'] : 0;
$user->quotes = isset($quotes[$user['CallCentreID']]['quotes']) ? $quotes[$user['CallCentreID']]['quotes'] : 0;
return $user;
});

Resources