I have a collection of items of two objects. I want to store the zero index's date in a variable such as $starttime,
and I want to store the first index's date in the variable such as $endtime
. How can I do that?
#items: array:2 [▼
0 => {#820 ▼
"id": 75
"code": ""8080"
"name": "Ahmed"
"date": "2021-12-01 08:57:40"
"created_at": "2022-02-09 11:45:30"
"updated_at": "2022-02-09 11:45:30"
}
1 => {#822 ▼
"id": 76
"code": ""8080"
"name": "Ahmed"
"date": "2021-12-01 18:14:13"
"created_at": "2022-02-09 11:45:30"
"updated_at": "2022-02-09 11:45:30"
}
]
#escapeWhenCastingToString: false
Controller
$data = DB::table('data')->orderBy('date', 'asc')->get();
foreach ($data as $datas) {
$dates = date('y-m-d', strtotime($datas->date));
$date = DB::table('data')->where('date', 'LIKE', '%'.$dates.'%')->get();
$finddate = DB::table('data')->where('date', 'LIKE', '%'.$dates.'%')->where('code', $datas->code)->get();
foreach ($finddate as $datefound) {
$starttime = $datefound->date;
$endtime = $datefound->date;
dd($endtime);
}
}
I am trying to do this, but it only shows the first date, such as the following.
2021-12-01 08:57:40
CodePudding user response:
You might want to add an expected result because your question is quite confusing. If you want to store the first two rows of the result of the query into a variable, you can try this:
$data = DB::table('data')->orderBy('date')->get();
if (sizeof($data) >= 2) { // Making sure result has atleast 2 rows
$starttime = $data[0];
$endtime= $data[1];
}