验证付款和Laravel培训的最高限额 [英] Verification of payments and ceiling of trainings in Laravel

查看:46
本文介绍了验证付款和Laravel培训的最高限额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My goal is that when I encode a student for a payment. He (student) can follow 2 trainings per payment. (this is the ceiling - 1 payment = 2 trainings)

在我的付款方式付款中,我为学生编码,例如Menier.

In my form Payment, I encode a student for example Menier.

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

The student is entitled to two trainings, (this is the ceiling)

以我的形式培训:我为学生Menier编码了2种培训.

In my form Training: I encode 2 trainings for the student Menier.

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

My first question: how can I block the number of trainings by two?

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

(so, if I encode another training, it must block!)

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

My second queston, if I encode 2 payments for the same student. The student is entitled to 4 trainings. How to create this type of algorithm?

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

Here is my code for now, I know it's not a lot...

编辑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的初学者.

Do you have an idea of how I could solve my problems, I am still a beginner in laravel.

对于Watercayman

谢谢您的时间.

推荐答案

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

This is not too bad to do. Since the payment is not directly associated with a specific training (ie you have a credit system), you can do this pretty easily with a couple of queries.

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

My first question: how can I block the number of trainings by two?

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

Start with the basics and find the number of trainings in the database for this student:

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

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

Or you can come in from the reverse for this simply query:

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

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

Then, to limit to two trainings (without payment consideration yet):

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

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

Now that you have a count of trainings, if you also want to make sure they have a payment in the system, lets get the payments:

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

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

To check if they have paid for trainings, you now have both pieces of data that you need. You just have to figure out if they have paid for the right amount of trainings based on 2 payments = 1 training. So:

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

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

My second queston, if I encode 2 payments for the same student. The student is entitled to 4 trainings. How to create this type of algorithm?

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

The above will work for 2 or 4 or whatever you want.

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

Now, if you want to enforce a max of 2 trainings per each payment, we can check on this too. BUT, this is starting to get a little complex or circular in the logic. If you can avoid this, it will be a lot easier. But, let's check on the max of 2 per payment, which is just the adding an equals check, AFTER the one above:

 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项培训并已付款,则可能是付款模型spent上的boolean字段(或表明付款不再有效的信息).如果您不需要历史数据,也可以从系统中删除付款.但是,假设您这样做了,并且使用了$payment->spent,您仍然可以执行上述算法,只需将花掉的行添加到查询中,例如:

This should solve your issue. However, you didn't ask, but I assume you don't want a student to allow a training if they have already used up a payment. IE if they've taken a training and they have 'spent their credit', they should not be allowed to take the training. If this is important to you, I suggest that in another part of your program, you write to the database when a payment has been consumed. So - if a student uses 2 trainings and has paid for them, maybe a boolean field on the Payment model spent (or something to indicate the payment is no longer valid). You could also remove the payment from the system if you don't need historical data. But, assuming you do, and you use $payment->spent, you can still do the above algorithm, just add the spent line to the query something like:

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

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

Then all the rest should be the same. This isn't cut & paste, but I think now that you have separated out payments and trainings, this should be a pretty easy solve to understand based on the above. :)

这篇关于验证付款和Laravel培训的最高限额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆