I have a Collection with the following structure;
Collection {#6467 ▼
#items: array:7 [▼
0 => Appointment {#6459 ▼
...
#attributes: array:9 [▼
"id" => 1092
"company_id" => 2
"employee_id" => 5
"client_id" => 31
"notes" => "Fugiat reprehenderit cumque rerum error quia aliquam ut autem dolor doloremque adipisci est fugiat quia dolorum ut voluptates itaque qui impedit enim nobis."
"startDateTime" => "2019-11-18 11:00:00"
"endDateTime" => "2019-11-18 12:00:00"
"created_at" => "2019-11-15 18:55:55"
"updated_at" => "2019-11-15 18:55:55"
]
#original: array:9 []
....
1 => All other appointments...
In frontend I need the duration of the appointment. At the moment I am making a foreach loop gping through each appointment,
calculate the duration like this:
foreach( $appointments as $appointment)
$duration = $appointment->endDateTime - $appointment->startDateTime
//Add to the appointment
$appointment->duration = $duration
}
It works but since I have potentially hundreds of Appointments (10 per day per each employee per each company) this process slows down the app.
So I wonder if I could calculate the duration (endDateTime - startDateTime) in the query and add it to the collection. So I did this:
$newDay = Appointment::>whereDate('startDateTime', 'like', '%' . $datum . '%')
->orderBy('startDateTime')
->select(
'appointments.*',
'duration',
function ($q) {
$q->raw('appointment->endDateTime' - 'appointment->startDateTime');
}
)
->get();
But I do get the following error:
stripos() expects parameter 1 to be string, object given
What am I doing wrong?