Duplicate system on two forms

前端 未结 2 1176
别那么骄傲
别那么骄傲 2020-12-22 08:40

My project is to create a small driving school for motorbike.

I have 4 tables: Former, Training, Revision, Motorbike

My functions cre

2条回答
  •  误落风尘
    2020-12-22 09:21

    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();
    

提交回复
热议问题