david2000
1 week ago

condition on a foreign key

Posted 1 week ago by david2000

I have to make 2 conditions:

  1. My first condition: I have to test that if I don't have a payment, I don't can encode a training.

  2. Then, my second condition (very difficult for me), the number of seance must be equal to the number of reservation. For example: 1 seance = 1 training. For now, my bookings are unlimited... :-(

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


    }

My tables are:

Training = date_sitting, fk_payment, fk_student

Payment = date_payment, number_seance , price, total, fk_student

Please sign in or create an account to participate in this conversation.