Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

david2000's avatar

the number of training is equal to the reservation

I am stuck since several days concernng the checking betwen the number of training and the reservation.

For example: you pay 1 training (1 seance), you have 1 booking. we can not encode 2 bookings an error message appears. It's my but...

in my table Training, here is my fields (hour_start, hour_end, fk_motorbike, fk_former, fk_student, fk_payment)

and In my table Payment I have this (date_payment, number_seance, total, fk_student, method_payment)

In my screenshot, I have 1 payment for 1 seance

Here, the problem is that I can add several trainings, it's not limit to 1 seance.

do you have any idea how I could do that? I tried this but without success

$seance = Payment::find($request->fk_payment);

        if($request->seance >= $request->number_seance){ 
             return redirect()->route('trainings.index')
            ->with('error', 'Limit! ');
        }
0 likes
6 replies
Nakov's avatar

In your if statement you are checking twice a number from the $request and never used the $seance from the Payment that you found. Is that a mistake only in your question here, or is it what you have in code as well?

Talking about this:

if($request->seance >= $request->number_seance){ 

// maybe it should be this
if($request->seance >= $seance->number_seance){ 

1 like
david2000's avatar

@nakov: my code is not correct visibly, would you have a little idea of how I could check my seances? I am stuck...

Nakov's avatar

@david2000 I think that your relationship is set in the wrong direction. You should have a foreign key to the training in the payment table, not vice versa. But anyway, if you already store the payment for the training correctly, then just checking if that field is empty or not will give you the answer that you are looking for, as far as I understand your problem..

So check for the training if fk_payment is null or not.

1 like
david2000's avatar

In my model I have this:

Training

public function payments(){

      return $this->belongsTo('App\Payment', 'fk_payment');
    }

Payment

public function Trainings(){

        return $this->hasMany('App\Training', 'fk_payment');
    }

My relationship is correct?

Snapey's avatar

Not related to the question, but I'm really curious why motorbikes and seances are managed in one application?

motorbikes, two wheeled motorised vehicle

seances, supposed method to communicate with spirits / the afterlife

How? What?

Please or to participate in this conversation.