I trying to get the data in mysql database, so i want to get all of the data even the date field is null, this is my code
$prod = DB::table('daily_enters')
->leftJoin('daily_inputs', 'daily_enters.daily_input_id', '=', 'daily_inputs.id')
->select('daily_inputs.date',DB::raw('sum(daily_enters.crops) as crops_actual'))
->whereIn('daily_inputs.date', $dates)
->where('daily_inputs.divisi', 'A')
->groupBy('daily_inputs.date','daily_inputs.divisi')
->get();
this the result
^ IlluminateSupportCollection {#1403 ▼
#items: array:30 [▼
0 => {#1402 ▼
+"date": "2022-01-02"
+"crops_actual": 353.91
}
1 => {#1407 ▼
+"date": "2022-01-03"
+"crops_actual": 465.2
}
2 => {#1405 ▶}
3 => {#1406 ▶}
4 => {#1410 ▶}
5 => {#1408 ▶}
6 => {#1409 ▶}
7 => {#1412 ▶}
8 => {#1413 ▶}
9 => {#1411 ▶}
10 => {#1414 ▶}
11 => {#1415 ▶}
12 => {#1416 ▶}
13 => {#1417 ▶}
14 => {#1418 ▶}
15 => {#1419 ▶}
16 => {#1420 ▶}
17 => {#1421 ▶}
18 => {#1422 ▶}
19 => {#1423 ▶}
20 => {#1424 ▶}
21 => {#1425 ▶}
22 => {#1426 ▶}
23 => {#1427 ▶}
24 => {#1428 ▶}
25 => {#1429 ▶}
26 => {#1430 ▶}
27 => {#1431 ▶}
28 => {#1432 ▶}
29 => {#1433 ▶}
]
#escapeWhenCastingToString: false
}
the data is return for 31 days in month but in 2022-01-01 is not found because its not have a data, but in this case i want to show the ‘2022-01-01’ with data return = 0
so if you an any idea for this issue, please tell me the correct way for solve that issue. thank you
2
Answers
this is the another way that i was trying
This is the result
this results that what i want, but the question is how to foreach that array into blade?
I think best if you let laravel handle the date range, for example using
$dateRange = CarbonPeriod::create($startDate, $endDate)->toArray();
And with the existing query, you get add
->get()->keyBy('daily_inputs.date');
After that, just looping through $dateRange
Hope this can help you.
NOTE:
as mention by @takumabo, you can also use laravel helper
data_get()
and if daily_inputs.date type is datetime you can format it when used as key