I'm doing an update function for a table with a many-to-many relationship, this is what I have:
tooth
- id
- name
cost
- id
- name
cost_tooth
- id
- tooth_id
- cost_id
- row_number
This is what I save in the intermediate table:
enter image description here
Column row_number is interpreted as row, row_number = 1 will be interpreted as row 1, row_number = 2 will be interpreted as row 2
Here's a picture of what I'm talking about
enter image description here
You will see what i say in the column TOOTH NAME
and now i want when i edit something in the column TOOTH NAME as below.
This is the value I get when I edit.
$output = [
0 => [
"tooth_id" => "1"
"row_number" => 1
]
1 => [
"tooth_id" => "2"
"row_number" => 1
]
2 => [
"tooth_id" => "1"
"row_number" => 2
]
3 => [
"tooth_id" => "2"
"row_number" => 2
]
4 => [
"tooth_id" => "3"
"row_number" => 2
]
5 => [
"tooth_id" => "4"
"row_number" => 2
]
]
$cost->tooths()->sync($output);
After I edited, the data in my database was messed up and not as I expected
enter image description here
Do you have a good solution? help me
In an eCommerce store we have multiple transactions that relate to a specific order, e.g an order was made for 2 SKU's (1 transaction), 2 skus were added (another transaction), 1 was refunded (another transaction). The order itself is made up of multiple line items, each with a unique id.
Each of these transactions will have a numerical value, where together they will add up to the total of that order. What I'm trying to achieve is to take the line items and match their values to the transactions where there is no overlap (e.g a line item can only be part of one transaction).
A real world example would be as follows, there are 3 transactions -
Transaction 1 (6-671461867600) For 39.68
Transaction 2 (6-671717458000) For 31.98
Transaction 3 (6-671826772048) For -7.7
(these total 63.96)
And the line items which make up these transactions are as follows.
$lineItems = [
0 => [
"xId" => 96909066320
"price" => "-7.70"
"sku" => 'KB-291'
]
1 => [
"xId" => 4978455609424
"price" => "15.99"
"sku" => "261-R4-BE3B-CDWM"
]
2 => [
"xId" => 4978455642192
"price" => "15.99"
"sku" => "261-LV-5PT8-V6KA"
]
3 => [
"xId" => 4979504119888
"price" => "15.99"
"sku" => "261-UQ-3TEP-GJ8U"
]
4 => [
"xId" => 4979504152656
"price" => "15.99"
"sku" => "261-SO-58DE-0QKS"
]
5 => [
"xId" => 1887799246928
"price" => "7.70"
"sku" => 'KB-297'
]
];
What I'm trying to achieve is to specify which line items will make up each transaction, doing it manually I can find the follow work with no overlaps.
Transaction 1) 1887799246928, 4979504152656, 4979504119888
Transaction 2) 4978455642192, 4978455609424
Transaction 3) 96909066320
$array = [
"6-671826772048" => [
0 => "96909066320"
]
"6-671461867600" => [
0 => "1887799246928,4978455609424,4978455642192"
1 => "1887799246928,4978455609424,4979504119888"
2 => "1887799246928,4978455609424,4979504152656"
3 => "1887799246928,4978455642192,4979504119888"
4 => "1887799246928,4978455642192,4979504152656"
5 => "1887799246928,4979504119888,4979504152656"
]
"6-671717458000" => [
0 => "96909066320,1887799246928,4978455609424,4978455642192"
1 => "96909066320,1887799246928,4978455609424,4979504119888"
2 => "96909066320,1887799246928,4978455609424,4979504152656"
3 => "96909066320,1887799246928,4978455642192,4979504119888"
4 => "96909066320,1887799246928,4978455642192,4979504152656"
5 => "96909066320,1887799246928,4979504119888,4979504152656"
6 => "4978455609424,4978455642192"
7 => "4978455609424,4979504119888"
8 => "4978455609424,4979504152656"
9 => "4978455642192,4979504119888"
10 => "4978455642192,4979504152656"
11 => "4979504119888,4979504152656"
]
]
Where I've got to thus far is for every transaction, find what combination of line items would total the amount of the transaction - however I obviously need to ensure that there is no overlap,
e.g.
Transaction id 6-671826772048 can ONLY be calculated using Line Item 96909066320.
-Transaction id 6-671717458000 has an option of being calculated using 96909066320,1887799246928,4979504119888,4979504152656 - however, that would stop Transaction 1 from working.
To sumamrise, I want to take this array
$array = [
"6-671826772048" => [
0 => "96909066320"
]
"6-671461867600" => [
0 => "1887799246928,4978455609424,4978455642192"
1 => "1887799246928,4978455609424,4979504119888"
2 => "1887799246928,4978455609424,4979504152656"
3 => "1887799246928,4978455642192,4979504119888"
4 => "1887799246928,4978455642192,4979504152656"
5 => "1887799246928,4979504119888,4979504152656"
]
"6-671717458000" => [
0 => "96909066320,1887799246928,4978455609424,4978455642192"
1 => "96909066320,1887799246928,4978455609424,4979504119888"
2 => "96909066320,1887799246928,4978455609424,4979504152656"
3 => "96909066320,1887799246928,4978455642192,4979504119888"
4 => "96909066320,1887799246928,4978455642192,4979504152656"
5 => "96909066320,1887799246928,4979504119888,4979504152656"
6 => "4978455609424,4978455642192"
7 => "4978455609424,4979504119888"
8 => "4978455609424,4979504152656"
9 => "4978455642192,4979504119888"
10 => "4978455642192,4979504152656"
11 => "4979504119888,4979504152656"
]
]
and ONE result could be (although there are other correct possibilities)
$array = [
"6-671826772048" => "96909066320",
"6-671461867600 => "4978455642192, 4978455609424"
"6-671717458000" => "1887799246928, 4979504152656, 4979504119888"
];
If anyone has an idea on how to solve this it would be appreciated
Update - In the example I gave, whilst I presented one combination which solved the problem, there are many, there is no specific link between the transaction and line items, so as long as each transaction line items total the amount, and no transactions share line items, then the problem is considered solved
e.g the following would also be a valid solution
$array = [
"6-671826772048" => "96909066320",
"6-671461867600" => "4979504152656, 4979504119888"
"6-671717458000" => "4978455642192, 4978455609424, 1887799246928"
];
I have array of days and their open time and close time as below, I need to store these days and the open and close time for a selected store in table, in the same time if the store have already entered in this table then update the current records so no duplicate will be found
I hope you can help me in this
many thanks
"store_id" => "625"
"day_id" => array:7 [
1 => "1"
2 => "2"
3 => "3"
4 => "4"
5 => "5"
6 => "6"
7 => "7"
]
"open_time" => array:7 [
1 => "13:20"
2 => "01:20"
3 => "13:20"
4 => "01:20"
5 => "15:50"
6 => "07:20"
7 => "23:20"
]
"close_time" => array:7 [
1 => "20:20"
2 => "08:20"
3 => "20:20"
4 => "21:20"
5 => "18:20"
6 => "00:20"
7 => "19:20"
]
I tried with this but need more
public function store(Request $request)
{
$store_id=$request->storeinfo_id;
foreach ($request->input('day_id') as $key => $val) {
$array = [
'day_id' => $val,
];
Storeday::where('storeinfo_id',$store_id)->create($array);
}
return response()->json(['data' => trans('message.success')]);
}
I'm trying to sum a value of an array with the same employee no.
Here's the example of the array I'm trying to sum.
0 => array:10 [▼
"employee_no" => "04052018"
"employee_id" => 317
"company_id" => 4
"name" => ""
"monthly_salary" => 14000.0
"daily_salary" => 537.0
"work_hours" => 8
"sss_deduction" => 0
"pagibig_deduction" => 100
"philhealth_deduction" => 192
and
0 => array:10 [▼
"employee_no" => "04052018"
"employee_id" => 317
"company_id" => 4
"name" => ""
"monthly_salary" => 14000.0
"daily_salary" => 537.0
"work_hours" => 8
"sss_deduction" => 0
"pagibig_deduction" => 100
"philhealth_deduction" => 192
they are duplicates array in which i want to get the sum of work hours only to have this result
0 => array:10 [▼
"employee_no" => "04052018"
"employee_id" => 317
"company_id" => 4
"name" => ""
"monthly_salary" => 14000.0
"daily_salary" => 537.0
"work_hours" => 16
"sss_deduction" => 0
"pagibig_deduction" => 100
"philhealth_deduction" => 192
There are about 24 and more occurrences if this array in my result set in which i want to merge as one summing up the total work hours
Tried this one but getting no luck
foreach ($employee_attendance as $row){
if($row['employee_id'] === $row['employee_id']){
$payroll_data['work_hours'] += $row['total_hours'];
}
$payroll[] = $payroll_data;
}
The best would be to get it sorted out from Eloquent queries itself. However, if you wish, you can also use Collections' methods such as groupBy and each to filter the values based on employee ID and do a sum on them. Finally, have a result variable to store all the results.
Snippet:
$result = [];
$arr = $arr->groupBy('employee_no')->each(function($item,$key) use (&$result){
$sum = $item->sum('work_hours');
$emp_data = $item->first();
$emp_data['work_hours'] = $sum;
$result[] = $emp_data;
});
Full Code:
<?php
$arr = collect([
[
"employee_no" => "04052018",
"employee_id" => 317,
"company_id" => 4,
"name" => "",
"monthly_salary" => 14000.0,
"daily_salary" => 537.0,
"work_hours" => 8,
"sss_deduction" => 0,
"pagibig_deduction" => 100,
"philhealth_deduction" => 192
],
[
"employee_no" => "04052018",
"employee_id" => 317,
"company_id" => 4,
"name" => "",
"monthly_salary" => 14000.0,
"daily_salary" => 537.0,
"work_hours" => 8,
"sss_deduction" => 0,
"pagibig_deduction" => 100,
"philhealth_deduction" => 192
],
[
"employee_no" => "04052019",
"employee_id" => 317,
"company_id" => 4,
"name" => "",
"monthly_salary" => 14000.0,
"daily_salary" => 537.0,
"work_hours" => 80,
"sss_deduction" => 0,
"pagibig_deduction" => 100,
"philhealth_deduction" => 192
]
]);
$result = [];
$arr = $arr->groupBy('employee_no')->each(function($item,$key) use (&$result){
$sum = $item->sum('work_hours');
$emp_data = $item->first();
$emp_data['work_hours'] = $sum;
$result[] = $emp_data;
});
print_r($result);
As what others said, it's better to group and sum those work hours when you retrieve it from the database.
But in case you still want to do it like what you explained above, it can be done like this although it might not be the best way
foreach ($employee_attendance as $row){
// Check if this is the first occurence of the employee number or not
if (!isset ($payroll_data[$row['employee_no']]['work_hours']) ) {
// First occurence will set the total hours to the current loop work hours
$payroll_data[$row['employee_no']]['work_hours'] = $row['work_hours'];
} else {
// Second occurence and so on will sum previous total work hours
$payroll_data[$row['employee_no']]['work_hours'] += $row['work_hours'];
}
}
And the $payroll_data structure will be like this:
[
"04052018" => [
"work_hours" => 16 // Total work hours of this employee number
],
// Repeat for other employee number
]
Hence, it's easier for you to check the employee number with their total working hours as well.
I have a multidimensional array that I get from DB. Array has a number of views by each hour that is logged in the DB as a view, and it looks like this:
array:11 [▼
0 => array:2 [▼
"hour" => 0
"views" => 1
]
1 => array:2 [▼
"hour" => 1
"views" => 1
]
2 => array:2 [▼
"hour" => 4
"views" => 1
]
...and so on
]
I need to make a new array that will contain number of views for range of 2 hours. So for example from the array shown above I would like to get an array with, number of views for time between 0-2, that would 2, and for 2-4, would be 0 in this case, and so on.
You can do it in Mysql query:
select floor(hour/2) range, sum(views) sum
from thetable
group by range
You can just use a foreach to create a new array.
<?php
$your_array = [0 => [
"hour" => 0,
"views" => 4
],
1 => [
"hour" => 1,
"views" => 12
],
2 => [
"hour" => 4,
"views" => 1
],
3 => [
"hour" => 2,
"views" => 9
],
4 => [
"hour" => 21,
"views" => 19
]
];
foreach ($your_array as $value){
for($i=0;$i<=22;$i=$i+2){
$j=$i+2;
if($value['hour']>=$i && $value['hour']<$j){
isset($result[$i.'-'.$j])?$result[$i.'-'.$j]+=$value['views']:$result[$i.'-'.$j]=$value['views'];
}
}
}
print_r($result);
Try below code.
$arr = [0 => [
"hour" => 0,
"views" => 1
],
1 => [
"hour" => 1,
"views" => 1
],
2 => [
"hour" => 4,
"views" => 1
]];
foreach($arr as $row)
{
if($row['hour'] >= 0 && $row['hour'] <= 2)
{
$newArr['0-2'] = isset($newArr['0-2']) ? ($newArr['0-2'] + 1) : 1;
}
if($row['hour'] > 2 && $row['hour'] < 4)
{
$newArr['2-4'] = isset($newArr['2-4']) ? ($newArr['2-4'] + 1) : 1;
}
}
print_r($newArr);
Output
Array
(
[0-2] => 2
)