lundi 22 juillet 2019

Compare number of sittings on 2 models

My goal is to do a checking from form "trainings" via the form "payments".

If the student pays for 1 sitting.
So, In the form "trainings" I don't can add several sittings. I must to have an error message which block. Because the student paid for 1 sitting.

Form Payments enter image description here

Form Trainings enter image description here

I am stuck in my code... It's for the Controller Training.

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


        ]);


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

       if (!$exists){


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


        if(!isset($payment)){ 
            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');
        }
    }


    }

How Compare the number of seance please?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire