We've got a foreach loop that one of our console commands, it builds an array to be put in the database after being turned to Json.
In order to get one of the specific values, we'd like to reference two of the OTHER values that have been pulled in the loop
Below is the loop, the 'conversionRate' valus is what we're trying to get, from the result of the 'sales' and 'quotes' queries. I have no idea how to call those while still inside the loop.
foreach ($users as $user) {
$todaysCcActionsArray[] = [
'name' => DB::Table('sw_users')->where('EmailAddress', $user)->value('FirstName'),
'code' => $user,
'sales' => Order::where('Status', 'BOOKING')
->whereNotIn('Product', ['commercial_insurance', 'home_insurance'])
->where('MasterOrderNumber', 0)
->whereNull('OriginalOrderNumber')
->where('CallCentreID', '!=', $user)
->whereDate('OrderDate', '=', date('Y-m-d'))->count(),
'quotes' => Order::where('Status', 'QUOTE')
->whereNotIn('Product', ['commercial_insurance', 'home_insurance'])
->where('MasterOrderNumber', 0)
->whereNull('OriginalOrderNumber')
->where('CallCentreID', '!=', $user)
->whereDate('OrderDate', '=', date('Y-m-d'))->count(),
'conversionRate' => 'sales' / 'quotes' * 100
];
}
UPDATE
This Is how it comes out if printed.
array:16 [
0 => array:4 [
"name" => "Melissa"
"code" => "mkingnew"
"sales" => 2
"quotes" => 1
]
1 => array:4 [
"name" => "Wendy"
"code" => "wjonesnew"
"sales" => 2
"quotes" => 1
]
2 => array:4 [
"name" => "Sarah"
"code" => "sdickersonnew"
"sales" => 2
"quotes" => 1 ]
What you're effectively doing is trying to create an array entry using values from itself whilst you're creating it... you can't do that :)
What you can do is build the entry in pieces, then add it to the array:
foreach ($users as $user) {
$entry = array(
'name' => DB::Table('sw_users')->where('EmailAddress', $user)->value('FirstName'),
'code' => $user,
'sales' => Order::where('Status', 'BOOKING')
->whereNotIn('Product', ['commercial_insurance', 'home_insurance'])
->where('MasterOrderNumber', 0)
->whereNull('OriginalOrderNumber')
->where('CallCentreID', '!=', $user)
->whereDate('OrderDate', '=', date('Y-m-d'))->count(),
'quotes' => Order::where('Status', 'QUOTE')
->whereNotIn('Product', ['commercial_insurance', 'home_insurance'])
->where('MasterOrderNumber', 0)
->whereNull('OriginalOrderNumber')
->where('CallCentreID', '!=', $user)
->whereDate('OrderDate', '=', date('Y-m-d'))->count(),
);
$entry['conversionRate'] = $entry['sales'] / $entry['quotes'] * 100;
$todaysCcActionsArray[] = $entry;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加