Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a problem with my Laravel crud application for Registrations. There are these tables: Registration, ExtraRegistration (with a registration_id, extra_id and extraoptions_id), Extra and ExtraOptions (with the Extra_id).

In the RegistrationController when i add a Registration it makes a new record in the ExtraRegistration with the extraoptions_id and the extra_id. the extra_id is the name of the option and the extraoptions_id is the id of the option you selected.

But now, when you click on edit a record, it shows all the information. the problem is that when you change the extraoption, it makes another record, and not change the select. And when you have edited something and you look at it again, it still shows the option before you edited it.

RegistrationController

$options = Extra::where("exa_form_id", $distance->asd_form_id)->get();
    foreach($options as $option){
      $input_name = "option_" . $option->exa_id;
      $input_option = $request->$input_name;
      if(!is_null($input_option)){
         $input_name_extra = "extraoptions_" . $option->exa_id;
         $input_option_extra = $request->$input_name_extra;                      

         $registrationextra = new ExtraRegistration();
         $registrationextra->iea_registration_id = $registration->isg_id;
         $registrationextra->iea_extra_id = $input_option;
         $registrationextra->iea_extraoption_id = $input_option_extra;
         $registrationextra->iea_price = $option->exa_price;
         $registrationextra->save();

         }               

       }

$registration->isg_options = $input_option;
$registration->isg_option_extra_id = $input_option_extra;

Edit: I want a check before it makes a new ExtraRegistration. that if the price that is in the registration already exists in that registration_id(in ExtraRegistration) it doesnt make another record in the ExtraRegistration. I want to use an if to fix the problem, but not sure where to begin..

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
189 views
Welcome To Ask or Share your Answers For Others

1 Answer

It will probably be easier for you if you just use firstOrCreate method that Laravel provides out of the box.

You can check it here: https://laravel.com/docs/5.8/eloquent#other-creation-methods

That way, you will be able to retrieve the record that you are looking for, if there is any and then perform all the changes on it. If that record doesn't exist, Laravel will create the new record for you which you'll be able to update.

I hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...