Home > Enterprise >  how query get data between first years-months and last years-months selected laravel
how query get data between first years-months and last years-months selected laravel

Time:09-13

I have this example php array

Data = [
    [0] => array [
                    year=> '2020-01',
                    data1 => 100,
                    data2 => 101,   
                    data3 => 102,   
                 ],
    [1] => array [
                    year=> '2020-02',
                    data1 => 200,
                    data2 => 201,   
                    data3 => 202,  
                 ],
    [2] => array [
                    year=> '2020-03',
                    data1 => 300,
                    data2 => 301,   
                    data3 => 302,  
                 ]
    [3] => array [
                    year=> '2020-04',
                    data1 => 400,
                    data2 => 401,   
                    data3 => 402,  
                 ]
]

with this input request

$request1='2020-01';

$request2='2020-03';

I want to get data where year between $request1 and $request2, to be like this

 $result = [
    [0] => 100
    [1] => 101
    [2] => 102
    [3] => 200
    [4] => 201
    [5] => 202
    [6] => 300
    [7] => 301
    [8] => 302
]

this my php-laravel code:

$result = array();
for($i=0;$i<=(($request1-$request2) 1);$i  ){
    $result[] = Data::select('*')->where('year', '==' ,[$request1,$request2])->get();
}
dd($result);

the expected result is not the same what I want, please help, thank you

CodePudding user response:

You Can Try This Code

$data[] = Data::select('*')->whereBetween('month', [$request1,$request2])->get();
  • Related