My project is to create a small driving school for motorbike.
I have 4 tables: Former, Training, Revision, Motorbike
My functions cre
The easiest way is making use of a query scope in the Training model. I'll assume that a motorbike is the whole day unavailable when in revision.
public function scopeAvailable($query)
{
return $query->whereNotBetween('trainings.date_sceance', [
'revisions.date_revision_start' => function($q) {
return $q->where('trainings.fk_motorbike', '=', 'revisions.fk_motorbike');
},
'revisions.date_revision_end' => function($q) {
return $q->where('trainings.fk_motorbike', '=', 'revisions.fk_motorbike');
}]
);
}
I haven't test this code. It is possible it won't give what you expect, in other words, my proposition is wrong. Using the scope would be
$exists = Training::where('date_seance', $request->get('date_seance'))->available()->count();