samedi 6 juillet 2019

Add a training after payment

I have 3 form, the first is the form students with 2 fields (name, firstname)

enter image description here

Then, we have the form payments with 3 fields (date_encoding, price, fk_student)

enter image description here

And finally, the last form trainings with 3 fields (date_sitting, fk_student, fk_payment).

My problem is the form trainings, I can create a new record without the payment of a student.

enter image description here

Is it possible to block a record with an error message in the training form?

Here is an idea of my code for now.

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

        }   
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire