付款后添加培训

时间:2019-07-06 15:20:36

标签: laravel forms laravel-5

我有3个表单,第一个是students表单,其中有2个字段(名称,名字)

enter image description here

然后,我们以payments的形式包含3个字段(date_encoding,price,fk_student)

enter image description here

最后,最后一个具有3个字段(date_sitting,fk_student,fk_payment)的表单trainings

我的问题是培训表格,我可以创建新记录而无需支付学生费用。

enter image description here

是否可以在培训表格中阻止带有错误消息的记录?

现在这是我的代码的想法。

public function store(Request $request)
    {
        $request->validate([
                'date_sitting' => 'required|date',
                'fk_student' => 'required',
                'fk_payment' => 'required'

        ]);

        $exists = Training::where('date_sitting', $request->get('date_sitting'))->where('fk_student', $request->get('fk_student'))->where('fk_payment', $request->get('fk_payment'))->count();

       if (!$exists){
            Training::create($request->all());
            return redirect()->route('trainings.index')
                ->with('success', 'new data created successfully');
        }

        else{
            return redirect()->route('trainings.index')
                ->with('error', 'duplicate');

        }   
    }

1 个答案:

答案 0 :(得分:1)

当然-只需在创建块内添加if-check。如果用户没有付款,请不要创建并返回错误消息。您可以像重定向一样通过重定向来执行此操作,或者甚至可以通过AJAX进行单独的支票以仅查看是否付款,这类似于我在下面$payment添加的if-check。

但是,为了简单起见,请按以下步骤继续操作:

if (!$exists){
        // Is there a payment for the student trying to create a training?

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

        // You may wish to add some kind of date check to see if they paid for a specific training

        if(!isset($payment)){ // No payment = block creation and return error msg.
            return redirect()->route('trainings.index')
            ->with('error', 'No Payment, no training for you!');
        }
        else{
            Training::create($request->all());
            return redirect()->route('trainings.index')
                ->with('success', 'new data created successfully');
        }
    }