0

Right now i'm buliding export to csv function from db .

I was a long journey to complete it , a part of format or style in csv files is the to list the performance report by daily including the day without no sales at all . This is client request .

I have a lot of question to ask but now this the main priority .

I have query :

$period = DB::table('reports')->select('id','quantity','created_date')->whereBetween('created_date',$timerange)->where('branch_id',$br_id)->orderBy('created_date','asc')->get(); 

Result : (i'm using var_dump)

array (size=3)
 0 => 
array (size=2)
  'created_date' => string '2016-02-18 00:00:00' (length=19)
  'quantity' => int 1
1 => 
array (size=2)
  'created_date' => string '2016-02-20 00:00:00' (length=19)
  'quantity' => int 1
2 => 
array (size=2)
  'created_date' => string '2016-02-23 00:00:00' (length=19)
  'quantity' => int 1

The question is how to turn the result into this even there is no record on that date .

Example :

array (size=3)
0 => 
array (size=2)
  'created_date' => string '2016-02-18 17:21:04' (length=19)
  'quantity' => int 1
1 => 
array (size=2)
  'created_date' => string '2016-02-19 17:21:04' (length=19)
  'quantity' => int 0
2 => 
array (size=2)
  'created_date' => string '2016-02-20 00:00:00' (length=19)
  'quantity' => int 1
3 => 
array (size=2)
  'created_date' => string '2016-02-21 00:00:00' (length=19)
  'quantity' => int 0
4 => 
array (size=2)
  'created_date' => string '2016-02-22 00:00:00' (length=19)
  'quantity' => int 0
5 => 
array (size=2)
  'created_date' => string '2016-02-23 00:00:00' (length=19)
  'quantity' => int 1

Thank you

Muhammad Mu'az
  • 368
  • 2
  • 4
  • 14

1 Answers1

1

Okay I have checked the question after being edited. From what I understood, you need to include the dates that don't exist in the database in the query result. You have two options:

1st - Using DatePeriod PHP class to generate the dates between two dates:

$period = new \DatePeriod(
            new \DateTime('2010-10-01'),
            new \DateInterval('P1D'),
            new \DateTime('2010-10-05')
        );

And after the results are fetched from the database, you loop through all records and if a date existed in $period that didn't exist in the query result, you simply add it to the array of results in the same format retrieved from the Database.

2nd - You check the answer here

Hope this helps.

Community
  • 1
  • 1
Osama Sayed
  • 1,993
  • 15
  • 15