mercredi 9 octobre 2019

Validation on 2 models in laravel

I have a model Training with a recording

enter image description here

In my model Observation, I have to note the feedback of the training.

My problem is that I can add several observations even if I only have a training. I would like to better handle that with an error message.

enter image description here

For example, if I have 3 trainings, I can enter 3 observations only no 10 observations.

Here is an idea of my code for now

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

        ]);


       $instruction = $request->get('instruction'); 
       $description = $request->get('description');
       $fk_student = $request->get('fk_student');

       $trainings = Training::where('fk_student', $request->get('fk_student'))->first();


        if(!isset($trainings)){ 
            return redirect()->route('observations.index')
                    ->with('error', 'No training, no observation! ');
        }


        else{
            Observation::create($request->all());
                return redirect()->route('observations.index')
                    ->with('success', 'Add');
        }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire