i am new to PHP OOP , i have a database that contains products, the goal is to extract for every product grouped by ligne field all the months data of the products, after i executed the result i have been surprised that only the data of the month 8 is displayed while there are some products that have data in months like 4 or 6.
Here the database rows:
Here the json Result array and we can observe that some products qte in some months is displayed as zero while their qte is not zero in some cases
[
{
"id": "8",
"ligne": "Biscuit",
"produit": "Major",
"date": "2021-08-11",
"heure": "10:00",
"qte": "380",
"months": [
{
"month": 1,
"qte": 0
},
{
"month": 2,
"qte": 0
},
{
"month": 3,
"qte": 0
},
{
"month": 4,
"qte": 0
},
{
"month": 5,
"qte": 0
},
{
"month": 6,
"qte": 0
},
{
"month": 7,
"qte": 0
},
{
"month": 8,
"qte": 380
},
{
"month": 9,
"qte": 0
},
{
"month": 10,
"qte": 0
},
{
"month": 11,
"qte": 0
},
{
"month": 12,
"qte": 0
}
],
"year": "2021"
},
{
"id": "6",
"ligne": "Eau",
"produit": "Safia 1.5",
"date": "2021-08-11",
"heure": "14:00",
"qte": "320",
"months": [
{
"month": 1,
"qte": 0
},
{
"month": 2,
"qte": 0
},
{
"month": 3,
"qte": 0
},
{
"month": 4,
"qte": 0
},
{
"month": 5,
"qte": 0
},
{
"month": 6,
"qte": 0
},
{
"month": 7,
"qte": 0
},
{
"month": 8,
"qte": 320
},
{
"month": 9,
"qte": 0
},
{
"month": 10,
"qte": 0
},
{
"month": 11,
"qte": 0
},
{
"month": 12,
"qte": 0
}
],
"year": "2021"
},
{
"id": "12",
"ligne": "Lait",
"produit": "Vitalait 1/2",
"date": "2021-08-11",
"heure": "8:00",
"qte": "379",
"months": [
{
"month": 1,
"qte": 0
},
{
"month": 2,
"qte": 0
},
{
"month": 3,
"qte": 0
},
{
"month": 4,
"qte": 0
},
{
"month": 5,
"qte": 0
},
{
"month": 6,
"qte": 0
},
{
"month": 7,
"qte": 0
},
{
"month": 8,
"qte": 379
},
{
"month": 9,
"qte": 0
},
{
"month": 10,
"qte": 0
},
{
"month": 11,
"qte": 0
},
{
"month": 12,
"qte": 0
}
],
"year": "2021"
},
{
"id": "10",
"ligne": "Salami",
"produit": "Mazraa",
"date": "2021-08-11",
"heure": "8:00",
"qte": "570",
"months": [
{
"month": 1,
"qte": 0
},
{
"month": 2,
"qte": 0
},
{
"month": 3,
"qte": 0
},
{
"month": 4,
"qte": 0
},
{
"month": 5,
"qte": 0
},
{
"month": 6,
"qte": 0
},
{
"month": 7,
"qte": 0
},
{
"month": 8,
"qte": 570
},
{
"month": 9,
"qte": 0
},
{
"month": 10,
"qte": 0
},
{
"month": 11,
"qte": 0
},
{
"month": 12,
"qte": 0
}
],
"year": "2021"
},
{
"id": "4",
"ligne": "Yaourt",
"produit": "Delice",
"date": "2021-08-11",
"heure": "12:00",
"qte": "1020",
"months": [
{
"month": 1,
"qte": 0
},
{
"month": 2,
"qte": 0
},
{
"month": 3,
"qte": 0
},
{
"month": 4,
"qte": 0
},
{
"month": 5,
"qte": 0
},
{
"month": 6,
"qte": 0
},
{
"month": 7,
"qte": 0
},
{
"month": 8,
"qte": 1020
},
{
"month": 9,
"qte": 0
},
{
"month": 10,
"qte": 0
},
{
"month": 11,
"qte": 0
},
{
"month": 12,
"qte": 0
}
],
"year": "2021"
}
]
Here the Php Code :
public function getProductsStatsByMonths() {
$today = date("Y-m-d");
$date_arr = explode("-", $today);
$year = $date_arr[0];
$month = $date_arr[1];
$day = $date_arr[2];
$prods = array();
$months = array(
1 ,
2 ,
3 ,
4 ,
5 ,
6 ,
7 ,
8 ,
9 ,
10 ,
11 ,
12
);
$stmt = $this->conn->prepare("SELECT SUM(qte) as total , id ,ligne, produit, date, heure FROM production where YEAR(date) = ? GROUP BY LOWER(ligne) ");
$stmt->execute([$year]);
while( $row = $stmt->fetch(pdo::FETCH_ASSOC)){
$date_arrrow = explode("-", $row["date"]);
$rowmonth = $date_arrrow[1];
$ligne = $row["ligne"];
$produit = $row["produit"];
$p = new ProductionByMonth($row["id"],$row["ligne"],$row["produit"],$row["date"],$row["heure"],$row["total"],-1,$year);
$prodmonths = array();
foreach ($months as $month){
if(($rowmonth == $month) ){
array_push($prodmonths,new Months($month,(int) $row["total"]));
// array_push($prods, new ProductionByMonth($row["id"],$row["ligne"],$row["produit"],$row["date"],$row["heure"],$row["total"],$month,$year));
}else if ($rowmonth != $month){
array_push($prodmonths,new Months($month,0));
// array_push($prods, new ProductionByMonth($row["id"],$row["ligne"],$row["produit"],$row["date"],$row["heure"],0,$month,$year));
}
}
$p->months = $prodmonths;
array_push($prods,$p);
}
echo json_encode($prods);
}
My wish is to find a way to correctly extract all the correct qte in the json array for every product grouped by ligne field
I recommend grouping and aggregating the data entirely in the application. $grp is the core data structure. However, it needs a second pass for aggregation. Performance-wise, this is okay, as it is O(n).
I created a live example at http://road-to-eng.rlc.ninja/. I also recommend separating the aggregation logic from the presentation logic (unlike my implementation).
Here's the code:
<?php
// DB LOGIC
$pdo = new PDO("mysql:host=<redacted>;dbname=<redacted>", "<redacted>", "<redacted>");
$flig = $_POST["ligne"];
if (isset($flig) && $flig != "all") {
$sql = "select * from orders where ligne = ?;";
$params = [$flig];
} else {
$sql = "select * from orders;";
$params = [];
}
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$res = $stmt->fetchAll();
// GROUPING LOGIC
$lignes = [];
$grp = [];
foreach ($res as $ord) {
$mo = (int) substr($ord["date"], 5, 2);
$ligne = $ord["ligne"];
$lignes[$ligne] = null;
if (!isset($grp[$ligne])) {
$grp[$ligne] = [];
}
if (!isset($grp[$ligne][$mo])) {
$grp[$ligne][$mo] = [];
}
array_push($grp[$ligne][$mo], $ord);
}
$lignes = array_keys($lignes);
?>
<form action="" method="post">
<table>
<tr>
<td>
<select name="ligne">
<option value="all" selected>-- all --</option>
<?php foreach ($lignes as $l) { ?>
<option value="<?php echo $l ?>"><?php echo $l ?></option>
<?php } ?>
</select>
</td>
<td><input type="submit" /></td>
</tr>
</table>
</form>
<pre>
<?php
// PRESENTATION AND AGGREGATION LOGIC (tightly coupled, not good practice)
printf("<strong>YEAR %s</strong>\n", "2021");
printf("\n");
foreach ($grp as $ligne => $ords_of_mo) {
printf("%s\n", $ligne);
foreach ($ords_of_mo as $mo => $ords) {
printf("\t%s\n", date("M", mktime(0, 0, 0, $mo+1, 0, 0)));
// AGGREGATION LOGIC
$tot_qte = 0;
foreach ($ords as $o) {
$tot_qte += $o["qte"];
}
printf("\t\t%5s %10s %15s %15s %12s %10s\n", "id", "ligne", "produit", "date", "heure", "qte");
printf("\t\t========================================================================\n");
foreach ($ords as $o) {
printf("\t\t%5s %10s %15s %15s %12s %10s\n", $o["id"], $o["ligne"], $o["produit"], $o["date"], $o["heure"], $o["qte"]);
}
printf("\t\t%5s %10s %15s %15s %12s <strong>%10s</strong>\n", "", "", "", "", "", $tot_qte);
printf("\n");
}
}
?>
</pre>
Related
I have an response with this structure.
{
"records": [
{
"patientName": "CECILIA NONE ANCINEZ DE LOPEZ",
"patientProductCode": "MO000250",
"patientProduct": "RUXOLITINIB 15 MG TABLETA",
"patientFirstDispatch": "18/11/2021",
"dispatch": [
{
"month": 6,
"year": 2022,
"quantity": 30,
"period": "2022-6"
},
{
"month": 4,
"year": 2022,
"quantity": 30,
"period": "2022-4"
},
{
"month": 3,
"year": 2022,
"quantity": 30,
"period": "2022-3"
},
{
"month": 2,
"year": 2022,
"quantity": 30,
"period": "2022-2"
},
{
"month": 1,
"year": 2022,
"quantity": 30,
"period": "2022-1"
},
{
"month": 12,
"year": 2021,
"quantity": 30,
"period": "2021-12"
}
]
},
]
}
and I also have this json with the last 7 months.
{
"months": [
{
"month": "June",
"position": 6,
"year": 2022,
"period": "2022-6"
},
{
"month": "May",
"position": 5,
"year": 2022,
"period": "2022-5"
},
{
"month": "April",
"position": 4,
"year": 2022,
"period": "2022-4"
},
{
"month": "March",
"position": 3,
"year": 2022,
"period": "2022-3"
},
{
"month": "February",
"position": 2,
"year": 2022,
"period": "2022-2"
},
{
"month": "January",
"position": 1,
"year": 2022,
"period": "2022-1"
},
{
"month": "December",
"position": 12,
"year": 2021,
"period": "2021-12"
}
]
}
I need to add an object inside the dispatch array with the property
"quantity" = 0 when the "period" property doesn't match between the dispatch array and the months array.
the result of comparing between the array "months" and "dispatch" should return something like this.
An object was added at position 1 of the "dispatch" array.
{
"records": [
{
"patientName": "CECILIA NONE ANCINEZ DE LOPEZ",
"patientProductCode": "MO000250",
"patientProduct": "RUXOLITINIB 15 MG TABLETA",
"patientFirstDispatch": "18/11/2021",
"dispatch": [
{
"month": 6,
"year": 2022,
"quantity": 30,
"period": "2022-6"
},
{
"month": 5,
"year": 2022,
"quantity": 0,
"period": "2022-5"
},
{
"month": 4,
"year": 2022,
"quantity": 30,
"period": "2022-4"
},
{
"month": 3,
"year": 2022,
"quantity": 30,
"period": "2022-3"
},
{
"month": 2,
"year": 2022,
"quantity": 30,
"period": "2022-2"
},
{
"month": 1,
"year": 2022,
"quantity": 30,
"period": "2022-1"
},
{
"month": 12,
"year": 2021,
"quantity": 30,
"period": "2021-12"
}
]
},
]
}
I tried to do it but I could not add the objects correctly.
could you help me with this problem.
You can use forEach() and array_filter() for searching, then add new record to dispatch. Same as :
foreach ($objectMonths->months as $arrayMonth) {
foreach ($objectDispatch->records as $record) {
$exist = array_filter($record->dispatch, fn($mon) => $mon->period === $arrayMonth->period);
if (!count($exist)) {
$month = date('m', strtotime($arrayMonth->period));
$record->dispatch[] = (object)['month' => (int)$month, 'year' => $arrayMonth->year, 'quantity' => 0, 'period' => $arrayMonth->period];
usort($record->dispatch, fn($a, $b) => $a->period < $b->period);
}
}
}
Code sample in here
i am new in Php 7 , i am trying to write a function that checks in the database if a product has a qte in a certain month(extracted from date field) but in the case that in that the product date do not have this month so all data have to be added to the array but the qte must be set to zero to the value of that month , i got results only of the existing qte in the months , but the results of the months that the products do not have any qte are not in my final array
Here what i have tried:
public function getProductsStatsByMonths() {
$today = date("Y-m-d");
$date_arr = explode("-", $today);
$year = $date_arr[0];
$month = $date_arr[1];
$day = $date_arr[2];
$prods = array();
$months = array(
1 ,
2 ,
3 ,
4 ,
5 ,
6 ,
7 ,
8 ,
9 ,
10 ,
11 ,
12
);
foreach ($months as $month){
$stmt = $this->conn->prepare("SELECT sum(qte) as total,ligne, produit,date,heure,qte FROM production where YEAR(date) = ? and MONTH(date) = ? group by LOWER(ligne) ");
$stmt->execute([$year,$month]);
while( $row = $stmt->fetch(pdo::FETCH_ASSOC)){
array_push($prods, new ProductionByMonth(0,$row["ligne"],$row["produit"],$row["date"],$row["heure"],$row["qte"],$month,$year));
}
}
echo json_encode($prods);
}
the result im obtaining:
[
{
"id": 0,
"ligne": "Biscuit",
"produit": "Major",
"date": "2021-08-10",
"heure": "10:00",
"qte": "130",
"month": 8,
"year": "2021"
},
{
"id": 0,
"ligne": "Eau",
"produit": "Safia 1.5",
"date": "2021-08-10",
"heure": "14:00",
"qte": "200",
"month": 8,
"year": "2021"
},
{
"id": 0,
"ligne": "Lait",
"produit": "Vitalait 1/2",
"date": "2021-08-10",
"heure": "8:00",
"qte": "80",
"month": 8,
"year": "2021"
},
{
"id": 0,
"ligne": "Salami",
"produit": "Mazraa",
"date": "2021-08-10",
"heure": "8:00",
"qte": "100",
"month": 8,
"year": "2021"
},
{
"id": 0,
"ligne": "Yaourt",
"produit": "Delice",
"date": "2021-08-10",
"heure": "12:00",
"qte": "150",
"month": 8,
"year": "2021"
}
]
for example this row contains this product that have a qte in the month 8:
{
"id": 0,
"ligne": "Biscuit",
"produit": "Major",
"date": "2021-08-10",
"heure": "10:00",
"qte": "130",
"month": 8,
"year": "2021"
},
my goal is to add extra rows for this same product for the rest of the months but set qte for them as "0"
my goal is to make sure that my array in the end contains also the months that there is no qte (quantity) , and the value of qte must be set to zero and the data must be displayed like all data that exists except the value of the field qte
Here's a description of a general approach that works for this type of problem.
Get the unique products from the database data.
Create a calendar of all the dates needed.
Do the equivalent of a database "cross join" in PHP of the datasets from 1 and 2 and set qte to 0 for all combinations generated.
Overwrite qte for those combinations that actually exist in the database with a non-zero quantity.
"Combination" means product and date. So if you create a calendar that contains the 12 months of 2021 and have 5 products then step 3 will generate 60 combinations.
Also, if $day is not used anywhere in the code, there's no need to create it.
Edit - here is the code:
// this is modified sample data
// changes made:
// set date values to first day of each month
// added "productId" with fabricated values since "id" was 0 for everything in original data and therefore not useful
// added another entry for productID = 12 with a different date
$prods = '[
{
"id": 0,
"ligne": "Biscuit",
"produit": "Major",
"date": "2021-08-01",
"heure": "10:00",
"qte": "130",
"month": 8,
"year": "2021",
"productId": 12
},
{
"id": 0,
"ligne": "Biscuit",
"produit": "Major",
"date": "2021-09-01",
"heure": "12:00",
"qte": "158",
"month": 9,
"year": "2021",
"productId": 12
},
{
"id": 0,
"ligne": "Eau",
"produit": "Safia 1.5",
"date": "2021-08-01",
"heure": "14:00",
"qte": "200",
"month": 8,
"year": "2021",
"productId": 16
},
{
"id": 0,
"ligne": "Lait",
"produit": "Vitalait 1/2",
"date": "2021-08-01",
"heure": "8:00",
"qte": "80",
"month": 8,
"year": "2021",
"productId": 38
},
{
"id": 0,
"ligne": "Salami",
"produit": "Mazraa",
"date": "2021-08-01",
"heure": "8:00",
"qte": "100",
"month": 8,
"year": "2021",
"productId": 27
},
{
"id": 0,
"ligne": "Yaourt",
"produit": "Delice",
"date": "2021-08-01",
"heure": "12:00",
"qte": "150",
"month": 8,
"year": "2021",
"productId": 22
}
]';
// create an associative array of products - assign a unique value as key (this will be useful in a later step)
$products = [];
foreach (json_decode($prods, true) as $product) {
$key = $product['productId'] . '_' . $product['date'];
$products[$key] = $product;
}
// get unique product IDs from database data
$productIds = [];
foreach ($products as $product) {
$productIds[] = $product['productId'];
}
$uniqueProductIds = array_unique($productIds);
// create a calendar of all possible dates needed (just did a year's worth in this example)
$year = date("Y");
$buckets = [];
for ($month = 1; $month <= 12; $month++) {
$buckets[] = date($year . '-' . str_pad($month, 2, '0', STR_PAD_LEFT) . '-01');
}
// do cross join equivalent to create all combinations of bucket and unique product ID (in this example: 12 buckets x 5 unique products IDs = 60 combinations)
$cartesianProduct = [];
foreach ($buckets as $bucket) {
foreach ($uniqueProductIds as $uniqueProductId) {
$data = [];
$data['date'] = $bucket;
$data['id'] = $uniqueProductId;
$data['qte'] = 0;
$key = $uniqueProductId . '_' . $bucket;
$cartesianProduct[$key] = $data;
}
}
// overwrite combination data (in $cartesianProduct) with data from database (in $products) - this is where $key in $products and in $cartesianProduct is critical
$result = array_merge($cartesianProduct, $products);
// look at results
echo '<pre>' . print_r($result, 1) . '</pre>';
I am not able to understand the response for API in Laravel, it is confusing.
I expect this output
{
"month": "January",
"year": 2020,
"history_data": [
{
"id": 27,
"jurnal_id": 12313,
"name": "Transaksi RAHN FLEKSI",
"point": 500,
"status": "SUKSES",
"created_at": "2020-03-18 17:03:26",
"updated_at": "2020-03-18 17:03:26",
},
]
},
{
"month": "February",
"year": 2020,
"history_data": [
{
"id": 27,
"jurnal_id": 1231313,
"name": "Transaksi RAHN FLEKSI",
"point": 500,
"status": "SUKSES",
"created_at": "2020-03-18 17:03:26",
"updated_at": "2020-03-18 17:03:26",
}
],
{
"month": "February",
"year": 2021,
"history_data": [
{
"id": 182,
"jurnal_id": 13213,
"name": "Transaksi RAHN FLEKSI",
"point": 500,
"status": "SUKSES",
"created_at": "2021-02-18 17:03:26",
"updated_at": "2021-02-18 17:03:26",
},
{
"id": 1812313,
"jurnal_id": 12313313,
"name": "Transaksi RAHN FLEKSI",
"point": 500,
"status": "SUKSES",
"created_at": "2021-02-18 17:03:26",
"updated_at": "2021-02-18 17:03:26",
}
]
{
"month": "March",
"year": 2021,
"history_data": [
{
"id": 183,
"jurnal_id": 132313,
"name": "Transaksi RAHN FLEKSI",
"point": 500,
"status": "SUKSES",
"created_at": "2021-03-18 17:03:26",
"updated_at": "2021-03-18 17:03:26",
}
]
}
The 'history_data' is Grouped by year and month if I'm not wrong.
But, I still get response like below
"2021": [
{
"month": "February",
"year": "2021",
"history_data": {
"id": 2,
"id_jurnal": 1846756745650,
"name": "Transaksi ARRUM SAFAR",
"point": 1200,
"status": "SUKSES",
"created_at": "2021-02-13 10:11:21",
"updated_at": "2020-02-13 10:11:21",
"cif_number": "1003002713"
}
},
{
"month": "March",
"year": "2021",
"history_data": {
"id": 29,
"id_jurnal": 1015205749121113,
"name": "Transaksi RAHN FLEKSI",
"point": 500,
"status": "SUKSES",
"created_at": "2021-03-18 17:26:57",
"updated_at": "2020-03-18 17:26:57",
"cif_number": "1015205749"
}
}
],
"2020": [
{
"month": "February",
"year": "2020",
"history_data": {
"id": 1,
"id_jurnal": 1846756745652,
"name": "Transaksi ARRUM SAFAR",
"point": 1200,
"status": "SUKSES",
"created_at": "2020-02-13 09:52:56",
"updated_at": "2020-02-13 09:52:56",
"cif_number": "1003002713"
}
},
]
Here's the PHP / Laravel code
$historyPoints = $this->historyPoint
->select(
DB::raw("REGEXP_REPLACE(to_char(created_at, 'Month'), '\s+$', '') as month"),
DB::raw("date_part('Year', created_at) as year")
)
->groupBy('year', 'month')
->orderBy('year', 'desc')
->get();
$arr = [];
foreach($historyPoints as $historyPoint) {
foreach(HistoryPointAdd::all() as $data) {
$month = date('F', strtotime($data->created_at));
$year = date('Y', strtotime($data->created_at));
if($month == $historyPoint->month && $year == $historyPoint->year) {
$arr[] = [
'month' => $historyPoint->month,
'year' => $historyPoint->year,
'history_data' => $data
];
}
}
}
$makeToCollection = collect($arr)->groupBy('year');
return $this->sendSuccess($this->successMessage, $makeToCollection);
I'm using PostgreSQL for the database.
Thank you very much
The problem is :
$makeToCollection = collect($arr)->groupBy('year');
The groupBy method groups the collection's items by a given key
Just use :
$makeToCollection = collect($arr);
Btw, collection allows you to chain its methods to perform fluent mapping and reducing :
$historyPoints = $this->historyPoint
->select(
DB::raw("REGEXP_REPLACE(to_char(created_at, 'Month'), '\s+$', '') as month"),
DB::raw("date_part('Year', created_at) as year")
)
->groupBy('year', 'month')
->orderBy('year', 'desc')
->get();
/***** Here we go *****/
$makeToCollection = $historyPoints->map(function ($item) {
$history_data = [];
foreach (HistoryPointAdd::all() as $data) {
$month = date('F', strtotime($data->created_at));
$year = date('Y', strtotime($data->created_at));
if ($month == $historyPoint->month && $year == $historyPoint->year) {
$history_data[] = $data;
}
}
$item['history_data'] = $history_data;
return $item;
});
return $this->sendSuccess($this->successMessage, $makeToCollection);
I'm trying to do some data transformation through Laravel collection in my application for graphs. I want to create labels and datasets
I'm having a data something like this:
[
{
"id": 1,
"company_id": 1,
"year": 25,
"turnover": "3449",
"profit": "3201",
"turnover_range": 25,
"financial_year": {
"id": 25,
"year": "2024-25"
}
},
{
"id": 2,
"company_id": 1,
"year": 33,
"turnover": "5616",
"profit": "5905",
"turnover_range": 25,
"financial_year": {
"id": 33,
"year": "2032-33"
}
},
{
"id": 3,
"company_id": 1,
"year": 1,
"turnover": "4309",
"profit": "8563",
"turnover_range": 175,
"financial_year": {
"id": 1,
"year": "2000-01"
}
},
{
"id": 4,
"company_id": 1,
"year": 14,
"turnover": "5936",
"profit": "8605",
"turnover_range": 25,
"financial_year": {
"id": 14,
"year": "2013-14"
}
},
{
"id": 5,
"company_id": 1,
"year": 29,
"turnover": "7156",
"profit": "3844",
"turnover_range": 75,
"financial_year": {
"id": 29,
"year": "2028-29"
}
},
{
"id": 6,
"company_id": 1,
"year": 6,
"turnover": "5868",
"profit": "633",
"turnover_range": 75,
"financial_year": {
"id": 6,
"year": "2005-06"
}
},
{
"id": 7,
"company_id": 1,
"year": 25,
"turnover": "5809",
"profit": "6831",
"turnover_range": 575,
"financial_year": {
"id": 25,
"year": "2024-25"
}
},
{
"id": 8,
"company_id": 1,
"year": 12,
"turnover": "1",
"profit": "1976",
"turnover_range": 25,
"financial_year": {
"id": 12,
"year": "2011-12"
}
},
{
"id": 9,
"company_id": 1,
"year": 30,
"turnover": "680",
"profit": "1222",
"turnover_range": 25,
"financial_year": {
"id": 30,
"year": "2029-30"
}
},
{
"id": 10,
"company_id": 1,
"year": 26,
"turnover": "8197",
"profit": "3687",
"turnover_range": 25,
"financial_year": {
"id": 26,
"year": "2025-26"
}
}
]
Which is coming from eloquent collection of my model:
$fRA = FinancialAndRisk::whereHas('company', function($q) use($request){
$q->where('slug', $request->slug);
})
->with('financialYear')
->get();
I want to pluck out all the year inside financial_year as labels so i tried:
$labels = $fRA->pluck('financial_year.year');
it is showing null.
Again I tried
$labels= $fRA->map(function ($item){
$item->fin_year = $item->financial_year['year'];
return $item;
})->pluck('fin_year');
Even if I do transform I am getting the same null result,
Any ideas appreciated. Thanks
Edit:
Data format to be something like this:
labels = ['2000-01','2032-33','2024-25','2005-06'];
You should just be able to do something like:
$labels = $fRA->map(function ($item) {
return $item->financial_year->year;
})->unique();
Obviously, remove unique() if you want to include the duplicates (if there are any).
Alternatively, if you have the hasMany relationship set up in your FinancialYear model and you don't need $fRA after this you could just do:
$labels = FinancialYear::whereHas('FinancialAndRisk.company', function ($query) use($request) {
$query->where('slug', $request->slug);
})->pluck('year');
Below is rest api json output.
{
"data": {
"AB": [
{
"month": "Nov 2016",
"total_tax": "132"
},
{
"month": "Dec 2016",
"total_tax": 88
}
],
"BC": [
{
"month": "Nov 2016",
"total_tax": "132"
},
{
"month": "Dec 2016",
"total_tax": 76
}
],
"MB": [
{
"month": "Nov 2016",
"total_tax": "84"
},
{
"month": "Dec 2016",
"total_tax": 12
}
]
}
}
Above response is pass to jquery ajax function to plot highchart heatmap graph.We are trying to parse ajax response data to get the below pattern for heatmap graph.
var xaxisCategory = ['AB','BC','MB'];
var yaxisCategory = ['Nov 2016', 'Dec 2016'];
var series = [
[0, 0, 132], [0, 1, 88],
[1, 0, 132], [1, 1, 76],
[2, 0, 84], [2, 1, 12]
];
By using jquery inArray getting distinct x-axis and y-axis category.In series pattern having [x-axis,y-axis,value].In x-axis having 'AB' index is '0',on y-axis 'Nov 2016' index is '0' and values is total_tax is '132'.So how to plot series data.HoW to plot cartesian series data please help.
Try below example
var data = {
"data": {
"AB": [
{
"month": "Nov 2016",
"total_tax": 132
},
{
"month": "Dec 2016",
"total_tax": 88
}
],
"BC": [
{
"month": "Nov 2016",
"total_tax": 132
},
{
"month": "Dec 2016",
"total_tax": 76
}
],
"MB": [
{
"month": "Nov 2016",
"total_tax": 84
},
{
"month": "Dec 2016",
"total_tax": 12
}
]
}
};
var parsedata = data.data;
var xaxisCategory = Array();
var yaxisCategory = Array();
var series = Array();
jQuery.each( parsedata, function( key, value ) {
xaxisCategory.push(key);
jQuery.each( value, function( key, value ) {
yaxisCategory.push(value.month);
series.push(value.total_tax);
});
});
console.log(xaxisCategory);
console.log(yaxisCategory);
var final = Array();
var i = 0;
$.each(series, function( index, value ) {
var item = Array();
item.push(i);
if (index % 2 == 0) {
item.push(0);
}
else {
item.push(1);
}
if (index % 2 != 0) {
i++;
}
item.push(value);
final.push(item);
});
console.log(final);
Here is the jsfiddle see output in console