验证付款和培训上限

时间:2019-10-04 19:42:11

标签: php laravel laravel-5

我的目标是当我为payment的学生编码时。他(学生)可以按照2 trainings 每次付款 。 (这是最高限额-1次付款= 2次培训)

在我的表单付款中,我为一个学生编码,例如Menier

enter image description here

该学生有权参加两次培训(这是最高限额)

以我的表格培训:我为学生Menier编码了2种培训。

enter image description here

我的第一个问题:如何将培训次数减少两次?

(因此,如果我对另一种训练进行编码,则必须阻止!)

我的第二个任务,如果我为同一位学生编码了2次付款。该学生有权参加4次培训。如何创建这种算法?

enter image description here

这是我现在的代码,我知道不多...

编辑05-10-2019 -控制器培训

public function store(Request $request)
{
    $request->validate([
            'date_seance' => 'required',
            'hour_start' => 'required',
            'hour_end' => 'required',
            'fk_motorbike' => 'required',
            'fk_former' => 'required',
            'fk_student' => 'required',
            'fk_typeseance' => 'required'


    ]);


   $date_seance = $request->get('date_seance'); 
   $hour_start = $request->get('hour_start'); 
   $hour_end = $request->get('hour_end'); 
   $fk_motorbike = $request->get('fk_motorbike');
   $fk_student = $request->get('fk_student');
   $fk_former = $request->get('fk_former');
   $fk_typeseance = $request->get('fk_typeseance');


   $payments = Payment::where('fk_student', $request->get('fk_student'))->first();


    if(!isset($payments)){ 
        return redirect()->route('trainings.index')
                ->with('error', 'No payment, no training! ');
    }

    $thisStudentsTrainings = Training::where('fk_student', $fk_student)->get();


    if(count($thisStudentsTrainings) >= 2){ 
        return redirect()->route('trainings.index')
            ->with('error', 'The ceiling is 2 trainings! ');
    }

    $thisStudentsPayments = Payment::where('fk_student', $request->get('fk_student'))->get();


    if( (count($thisStudentsPayments) * 2) < count($thisStudentsTrainings)   ) {
         return redirect()->route('trainings.index')
                ->with('error', 'test!');
    }


    else{
        Training::create($request->all());
            return redirect()->route('trainings.index')
                ->with('success', 'Add');
    }



}

您是否知道如何解决问题,我还是laravel的初学者。

对于Watercayman

enter image description here enter image description here

谢谢您的时间。

1 个答案:

答案 0 :(得分:1)

这还不错。由于付款并不与特定的培训直接相关(例如,您拥有学分制),因此您可以通过几个查询轻松地做到这一点。

  

我的第一个问题:如何将培训次数减少两倍?

从基础开始,并在数据库中找到该学生的培训次数:

$thisStudentsTrainings = Training::where('fk_student', $fk_student)->get();

或者您可以从相反的方向进行此简单查询:

$student = Student::with('trainings')->get();
$thisStudentsTrainings = $student->trainings;

然后,将培训限于两次(尚未考虑付款):

if(count($thisStudentsTrainings) >= 2){ too many trainings }

现在您已经有了一些培训,如果您还想确保他们在系统中有付款,请让我们付款:

$thisStudentsPayments = Payment::where('fk_student', $request->get('fk_student'))->get();

要检查他们是否已支付培训费用,现在您拥有了两个所需的数据。您只需要确定他们是否按2笔付款= 1笔培训的价格支付了正确的培训费用。所以:

if( (count($thisStudentsPayments) * 2) < count($thisStudentsTrainings) ) {
    // They have not made enough payments!
}
  

我的第二个任务,如果我为同一位学生编码两次付款。该学生有权参加4次培训。如何创建这种算法?

以上将适用于2或4或任何您想要的。

现在,如果您希望每次付款最多执行2次培训,我们也可以对此进行检查。但是,这在逻辑上开始变得有些复杂或循环。如果您可以避免这种情况,它将更加容易。但是,让我们检查一下每笔付款的最大值2,这只是在上述付款之后添加一个等额支票:

 if( (count($thisStudentsTrainings) >= count($thisStudentsPayments) * 2) {
    // They are at their limit of trainings based on their payments!
    // Note we include >= so that if they have purchased 2 trainings, 
    // this blocks them from a 3rd until they pay again.
}

这应该可以解决您的问题。但是,您没有要求,但是我认为您不希望学生已经用完了付款就允许他们接受培训。 IE,如果他们接受了培训并且已经“花了功劳”,则不应允许他们接受培训。如果这对您很重要,我建议您在程序的另一部分中,在使用完付款后将其写入数据库。因此,如果一个学生使用2项培训并已付款,则 Payment 模型boolean上的spent字段(或表明付款不再有效的内容) 。如果您不需要历史数据,也可以从系统中删除付款。但是,假设您这样做了,并且使用了$payment->spent,您仍然可以执行上述算法,只需将花费的行添加到查询中,例如:

 $student = Student::with(['trainings' => function($query){
    $query->where('spent', 0)
 }])->get();

然后其余所有都应该相同。这不是剪切和粘贴,但是我认为既然您已经将付款和培训分开了,那么基于以上内容,这应该是一个很容易理解的解决方案。 :)

相关问题