I am using laravel query builder. i got stuck somewhere. My query is like below :-
$details = File::query();
if($ftskey=="partially"){
$details = $details->with(['partialfiles'=>function($query) use($startdate,$enddate){
$query->whereDate('partial_date', '>=', $startdate)
->whereDate('partial_date', '<=', $enddate);
}]);
}
$details = $details->get();
$response = json_decode(json_encode($details),true);
//Relationship below
public function partialfiles(){
return $this->hasMany('App\PartialFile','file_id');
}
Output is coming below:-
Array
(
[0] => Array
(
[id] => 238
[file_no] => FSC334
[partialfiles] => Array
(
[0] => Array
(
[id] => 3
[file_id] => 238
[partial_date] => 2019-09-23
[partial_amount] => 50
)
[1] => Array
(
[id] => 4
[file_id] => 238
[partial_date] => 2019-09-23
[partial_amount] => 50
)
)
)
[0] => Array
(
[id] => 239
[file_no] => FSC335
[partialfiles] => Array
(
[0] => Array
(
[id] => 3
[file_id] => 238
[partial_date] => 2019-09-23
[partial_amount] => 100
)
[1] => Array
(
[id] => 4
[file_id] => 238
[partial_date] => 2019-09-23
[partial_amount] => 100
)
)
)
)
Now i want to add sum of all partial_amount key that i used in hasMany relationship i.e is 50+50+100+100 =300 my expected output is like below:-
Array
(
[0] => Array
(
[id] => 238
[file_no] => FSC334
[total_amount] => 300
[partialfiles] => Array
(
[0] => Array
(
[id] => 3
[file_id] => 238
[partial_date] => 2019-09-23
[partial_amount] => 50
)
[1] => Array
(
[id] => 4
[file_id] => 238
[partial_date] => 2019-09-23
[partial_amount] => 50
)
)
)
[0] => Array
(
[id] => 239
[file_no] => FSC335
[total_amount] => 300
[partialfiles] => Array
(
[0] => Array
(
[id] => 3
[file_id] => 238
[partial_date] => 2019-09-23
[partial_amount] => 100
)
[1] => Array
(
[id] => 4
[file_id] => 238
[partial_date] => 2019-09-23
[partial_amount] => 100
)
)
)
)
Now see i have added the key total_amount in array. Kindly help me how can i get the sum of all amount.
note :- Please don't suggest me foreach loop to do. As i want to do at query level only.
You can use sum() from Collection
$details->partialfiles()->sum('partial_amount');
also read this documentation from laravel: https://laravel.com/docs/5.8/collections
Related
I have a Laravel collection with values, which I want to group by 'type' and sort by the biggest 'probability'. Here is how my collection looks like:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[type] => correct-score
[name] => 1-1
[probability] => 10.599257670442
[probability_odd] => 10.142219704667
[odd] =>
[value_bet] => 0
[prognose_type_id] => 12
[value_bet_diff] => -10.142219704667
)
[1] => Array
(
[type] => 3-way
[name] => 1
[probability] => 52.459602877727
[probability_odd] => 2.0491958402842
[prognose_type_id] => 1
[odd] => 2.48
[value_bet_diff] => 0.4308041597158
)
[2] => Array
(
[type] => 3-way
[name] => 2
[probability] => 24.682032371379
[probability_odd] => 4.3553949845985
[prognose_type_id] => 1
[odd] => 2.66
[value_bet_diff] => -1.6953949845985
)
[3] => Array
(
[type] => 3-way
[name] => X
[probability] => 22.857984044926
[probability_odd] => 4.7029519221256
[prognose_type_id] => 1
[odd] => 3.7
[value_bet_diff] => -1.0029519221256
)
[4] => Array
(
[type] => double-chance
[name] => 1X
[probability] => 37.658793461327
[probability_odd] => 2.8545789739758
[prognose_type_id] => 4
[odd] => 1.49
[value_bet_diff] => -1.3645789739758
)
[5] => Array
(
[type] => double-chance
[name] => X2
[probability] => 23.770008208152
[probability_odd] => 4.5225058005294
[prognose_type_id] => 4
[odd] => 1.55
[value_bet_diff] => -2.9725058005294
)
[6] => Array
(
[type] => double-chance
[name] => 12
[probability] => 38.570817624553
[probability_odd] => 2.7870811826288
[prognose_type_id] => 4
[odd] => 1.29
[value_bet_diff] => -1.4970811826288
)
[7] => Array
(
[type] => both-to-score
[name] => yes
[probability] => 58.505776168275
[probability_odd] => 1.8374254140447
[prognose_type_id] => 8
[odd] =>
[value_bet_diff] => -1.8374254140447
)
I want to achieve as result something like that:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => Array
(
[type] => correct-score
[name] => 1-1
[probability] => 10.599257670442
[probability_odd] => 10.142219704667
[odd] =>
[value_bet] => 0
[prognose_type_id] => 12
[value_bet_diff] => -10.142219704667
)
[1] => Array
(
[type] => 3-way
[name] => 1
[probability] => 52.459602877727
[probability_odd] => 2.0491958402842
[prognose_type_id] => 1
[odd] => 2.48
[value_bet_diff] => 0.4308041597158
)
[6] => Array
(
[type] => double-chance
[name] => 12
[probability] => 38.570817624553
[probability_odd] => 2.7870811826288
[prognose_type_id] => 4
[odd] => 1.29
[value_bet_diff] => -1.4970811826288
)
[7] => Array
(
[type] => both-to-score
[name] => yes
[probability] => 58.505776168275
[probability_odd] => 1.8374254140447
[prognose_type_id] => 8
[odd] =>
[value_bet_diff] => -1.8374254140447
)
Haven't been able to find similar solutions, so I finally decided to try here. Can you please help me?
Thanks in advance!
I think below can work;
$collection->sortByDesc('probability')->groupBy('type');
Sort by probability than group by type;
Probably better to do this in Eloquent, but as you have presented this as a collections problem;
assuming your collection is called $items
$sorted = $items->groupBy('type')
->map(function($group){
return $group->sortByDesc('probability')->take(1);
})
->flatten(1);
The most short:
$items = $collections
->sortByDesc('probability')
->unique('type');
I have the following code:
$s= $this->ShowFare->find('all');
Array
(
[0] => Array
(
[ShowFare] => Array
(
[id] => 21
[press_release_id] => 14
[category] => 0
[title] => pr
[image_name] => 1486719733.jpg
[crop_image] => 1486719729.jpg
[is_default] => 0
[is_active] => 0
[created] => 2017-02-10 09:42:13
[created_by] => 0
[modified] => 2017-02-10 09:42:13
[modified_by] => 0
[deleted] => 0000-00-00 00:00:00
[deleted_by] => 0
)
[PressRelease] => Array
(
[id] =>
[title] =>
)
)
[1] => Array
(
[ShowFare] => Array
(
[id] => 22
[press_release_id] => 7
[category] => 0
[title] => abcd
[image_name] => 1486721484.jpg
[crop_image] => 1486721481.jpg
[is_default] => 0
[is_active] => 0
[created] => 2017-02-10 10:11:24
[created_by] => 0
[modified] => 2017-02-10 10:11:24
[modified_by] => 0
[deleted] => 0000-00-00 00:00:00
[deleted_by] => 0
)
[PressRelease] => Array
(
[id] => 7
[title] => ht48 hours, 04 March 2016
)
)
)
I want the above results in following format:
Array
(
Array
(
[ShowFare] => Array
(
[id] => 21
[press_release_id] => 14
[category] => 0
[title] => pr
[image_name] => 1486719733.jpg
[crop_image] => 1486719729.jpg
[is_default] => 0
[is_active] => 0
[created] => 2017-02-10 09:42:13
[created_by] => 0
[modified] => 2017-02-10 09:42:13
[modified_by] => 0
[deleted] => 0000-00-00 00:00:00
[deleted_by] => 0
)
[PressRelease] => Array
(
[id] =>
[title] =>
)
)
Array
(
[ShowFare] => Array
(
[id] => 22
[press_release_id] => 7
[category] => 0
[title] => abcd
[image_name] => 1486721484.jpg
[crop_image] => 1486721481.jpg
[is_default] => 0
[is_active] => 0
[created] => 2017-02-10 10:11:24
[created_by] => 0
[modified] => 2017-02-10 10:11:24
[modified_by] => 0
[deleted] => 0000-00-00 00:00:00
[deleted_by] => 0
)
[PressRelease] => Array
(
[id] => 7
[title] => ht48 hours, 04 March 2016
)
)
)
Is this possible?
Thanks in advance...
My whole moto in this question is to make a new array from existing array and to reverse the first and second index of array.
I have an array
Array
(
[0] => Array
(
[0] => Array
(
[field] => Array
(
[name] => name
[tabindex] => 0
)
[colspan] => 3
)
)
[1] => Array
(
[0] => Array
(
[field] => Array
(
[name] => sequence
[tabindex] => 0
)
[colspan] => 3
)
)
[2] => Array
(
[0] => Array
(
[field] => Array
(
[name] => description
[tabindex] => 0
)
[colspan] => 3
)
)
[3] => Array
(
[0] => Array
(
[field] => Array
(
[name] => status
[tabindex] => 0
)
[colspan] => 3
)
)
[4] => Array
(
[0] => Array
(
[field] => Array
(
[name] => modified_by_name
[customCode] => {$fields.date_modified.value} {$APP.LBL_BY} {$fields.modified_by_name.value}
[label] => LBL_DATE_MODIFIED
[tabindex] => 0
)
[colspan] => 3
)
)
[5] => Array
(
[0] => Array
(
[field] => Array
(
[name] => created_by_name
[customCode] => {$fields.date_entered.value} {$APP.LBL_BY} {$fields.created_by_name.value}
[label] => LBL_DATE_ENTERED
[tabindex] => 0
)
[colspan] => 3
)
)
[6] => Array
(
[0] => Array
(
[field] => Array
(
[name] =>
)
)
[1] => Array
(
[field] => Array
(
[name] =>
)
)
)
)
Now i have to make a new array with first index on second and second on first index like this But have to do only in smarty not in php etc
Array
(
[0] => Array
(
[0] => Array
(
[field] => Array
(
[name] => name
[tabindex] => 0
)
[colspan] => 3
)
[1] => Array
(
[field] => Array
(
[name] => sequence
[tabindex] => 0
)
[colspan] => 3
)
[2] => Array
(
[field] => Array
(
[name] => description
[tabindex] => 0
)
[colspan] => 3
)
[3] => Array
(
[field] => Array
(
[name] => status
[tabindex] => 0
)
[colspan] => 3
)
[4] => Array
(
[field] => Array
(
[name] => modified_by_name
[customCode] => {$fields.date_modified.value} {$APP.LBL_BY} {$fields.modified_by_name.value}
[label] => LBL_DATE_MODIFIED
[tabindex] => 0
)
[colspan] => 3
)
[5] => Array
(
[field] => Array
(
[name] => created_by_name
[customCode] => {$fields.date_entered.value} {$APP.LBL_BY} {$fields.created_by_name.value}
[label] => LBL_DATE_ENTERED
[tabindex] => 0
)
[colspan] => 3
)
)
)
I think the following might help you.
You could loop the array you have and fill a new array with the values. Lets call the original $firstArray and the new one $newArray. I see that the first item from the subarrays are what you need(and there are no other items apart from 0) so you could do the following:
{$newArray = []}
{foreach $firstArray as $item}
{$newArray[] = $item.0}
{/foreach}
$item.0 selects the first subarray(there are no other). You might have to use $item[0] depending on your smarty version.
If there is more than 1 subarray, then loop $items first before you add an array to the $newArray.
It looks as if there's another array encapsuling your firstArray, so perhaps you need to select the subarray from the main one first....
note: I used smarty 3. Perhaps you use a different version, be sure to check the syntax for your version.
Array
(
[0] => stdClass Object
(
[id] => 16
[department_id] => 4
[employee_status] => 1
)
[1] => stdClass Object
(
[id] => 17
[department_id] => 8
[employee_status] => 1
)
)
i found a solution that sort object array
function cmp($a, $b) {
return $b->department_id - $a->department_id;
}
$sirtedArray=usort($employeelist, "cmp");
error_reporting(0);
session_start();
$link=$_POST['Host'];
$_SESSION['Host']=$link;
$client = new SoapClient("http://".$link."/api/soap/?wsdl");
/*api credentials*/
$apiuser=$_POST['User'];
$_SESSION['User']=$apiuser;
$apikey =$_POST['Key'];
$_SESSION['Key']=$apikey;
/*Action to be made */
$action =$_POST['Catalog'];
$_SESSION['Catalog']=$action;
/*Error trapping if in case number will be input*/
if(ctype_alpha(!$action))
{
die('Incorrect format');
}
else
{
$sess_id= $client->login($apiuser, $apikey);
/api login/
echo"Total Active list Product:".(count($client->call($sess_id, $action)));
echo"";
print_r($result=$client->call($sess_id,$action));
echo"";
}
?>
Array
(
[0] => Array
(
[product_id] => 1
[sku] => ROx12345
[name] => acer netbook
[set] => 4
[type] => simple
[category_ids] => Array
(
)
[website_ids] => Array
(
[0] => 1
)
)
[1] => Array
(
[product_id] => 2
[sku] => Cats1234
[name] => starbucks mug
[set] => 4
[type] => simple
[category_ids] => Array
(
)
[website_ids] => Array
(
[0] => 1
)
)
[2] => Array
(
[product_id] => 3
[sku] => samsung2141
[name] => samsung 3110
[set] => 4
[type] => grouped
[category_ids] => Array
(
)
[website_ids] => Array
(
[0] => 1
)
)