Adding a new record on a update field











up vote
0
down vote

favorite












I have a system where a user can edit their pets. On this page, they can also add another pet to their account.



This is what I used to edit the pet:



foreach ($request->pets as $pet) {
$clientPet = $client->pets()->find($pet)->first();
$clientPet->name = $pet['name'];
$clientPet->treated_at = $pet['treatedAt'];
$clientPet->save();

$date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
foreach($clientPet->reminders as $reminders) {
$reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
$reminders->product_id = $pet['productId'];
$reminders->application_id = $pet['applicationId'];
$reminders->save();
}
}


How would I account for creating a new pet alongside this?



Each pet also has reminders. Currently, reminders are updating in this method (previously having been created). How would I create the reminders for new pets. There are 4 reminders for each.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a system where a user can edit their pets. On this page, they can also add another pet to their account.



    This is what I used to edit the pet:



    foreach ($request->pets as $pet) {
    $clientPet = $client->pets()->find($pet)->first();
    $clientPet->name = $pet['name'];
    $clientPet->treated_at = $pet['treatedAt'];
    $clientPet->save();

    $date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
    foreach($clientPet->reminders as $reminders) {
    $reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
    $reminders->product_id = $pet['productId'];
    $reminders->application_id = $pet['applicationId'];
    $reminders->save();
    }
    }


    How would I account for creating a new pet alongside this?



    Each pet also has reminders. Currently, reminders are updating in this method (previously having been created). How would I create the reminders for new pets. There are 4 reminders for each.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a system where a user can edit their pets. On this page, they can also add another pet to their account.



      This is what I used to edit the pet:



      foreach ($request->pets as $pet) {
      $clientPet = $client->pets()->find($pet)->first();
      $clientPet->name = $pet['name'];
      $clientPet->treated_at = $pet['treatedAt'];
      $clientPet->save();

      $date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
      foreach($clientPet->reminders as $reminders) {
      $reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
      $reminders->product_id = $pet['productId'];
      $reminders->application_id = $pet['applicationId'];
      $reminders->save();
      }
      }


      How would I account for creating a new pet alongside this?



      Each pet also has reminders. Currently, reminders are updating in this method (previously having been created). How would I create the reminders for new pets. There are 4 reminders for each.










      share|improve this question















      I have a system where a user can edit their pets. On this page, they can also add another pet to their account.



      This is what I used to edit the pet:



      foreach ($request->pets as $pet) {
      $clientPet = $client->pets()->find($pet)->first();
      $clientPet->name = $pet['name'];
      $clientPet->treated_at = $pet['treatedAt'];
      $clientPet->save();

      $date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
      foreach($clientPet->reminders as $reminders) {
      $reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
      $reminders->product_id = $pet['productId'];
      $reminders->application_id = $pet['applicationId'];
      $reminders->save();
      }
      }


      How would I account for creating a new pet alongside this?



      Each pet also has reminders. Currently, reminders are updating in this method (previously having been created). How would I create the reminders for new pets. There are 4 reminders for each.







      php laravel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 13 hours ago

























      asked 14 hours ago









      Andre MacNamara

      1609




      1609
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You can use findOrNew, it will find the existing pet or instantiate a new instance.



          docs:



          foreach ($request->pets as $pet)
          {
          $clientPet = $client->pets()->findOrNew($pet['id']); // pass the pet id here
          $clientPet->name = $pet['name'];
          $clientPet->treated_at = $pet['treatedAt'];

          $clientPet->save();

          $date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
          foreach($clientPet->reminders as $reminders)
          {

          $reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
          $reminders->product_id = $pet['productId'];
          $reminders->application_id = $pet['applicationId'];

          $reminders->save();
          }
          }





          share|improve this answer























          • That creates the pets thanks. But it doesn't create the reminders (in the foreach loop). Any idea there?
            – Andre MacNamara
            13 hours ago










          • Are you passing the reminders with the request? At the moment your only getting them from the database.
            – Remul
            13 hours ago










          • So I have to create new ones and update them? How do I do that
            – Andre MacNamara
            13 hours ago










          • How are you passing the reminders in the request? can you update your question.
            – Remul
            13 hours ago












          • That updated based on a relationship with the pet
            – Andre MacNamara
            13 hours ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265991%2fadding-a-new-record-on-a-update-field%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          You can use findOrNew, it will find the existing pet or instantiate a new instance.



          docs:



          foreach ($request->pets as $pet)
          {
          $clientPet = $client->pets()->findOrNew($pet['id']); // pass the pet id here
          $clientPet->name = $pet['name'];
          $clientPet->treated_at = $pet['treatedAt'];

          $clientPet->save();

          $date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
          foreach($clientPet->reminders as $reminders)
          {

          $reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
          $reminders->product_id = $pet['productId'];
          $reminders->application_id = $pet['applicationId'];

          $reminders->save();
          }
          }





          share|improve this answer























          • That creates the pets thanks. But it doesn't create the reminders (in the foreach loop). Any idea there?
            – Andre MacNamara
            13 hours ago










          • Are you passing the reminders with the request? At the moment your only getting them from the database.
            – Remul
            13 hours ago










          • So I have to create new ones and update them? How do I do that
            – Andre MacNamara
            13 hours ago










          • How are you passing the reminders in the request? can you update your question.
            – Remul
            13 hours ago












          • That updated based on a relationship with the pet
            – Andre MacNamara
            13 hours ago















          up vote
          0
          down vote













          You can use findOrNew, it will find the existing pet or instantiate a new instance.



          docs:



          foreach ($request->pets as $pet)
          {
          $clientPet = $client->pets()->findOrNew($pet['id']); // pass the pet id here
          $clientPet->name = $pet['name'];
          $clientPet->treated_at = $pet['treatedAt'];

          $clientPet->save();

          $date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
          foreach($clientPet->reminders as $reminders)
          {

          $reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
          $reminders->product_id = $pet['productId'];
          $reminders->application_id = $pet['applicationId'];

          $reminders->save();
          }
          }





          share|improve this answer























          • That creates the pets thanks. But it doesn't create the reminders (in the foreach loop). Any idea there?
            – Andre MacNamara
            13 hours ago










          • Are you passing the reminders with the request? At the moment your only getting them from the database.
            – Remul
            13 hours ago










          • So I have to create new ones and update them? How do I do that
            – Andre MacNamara
            13 hours ago










          • How are you passing the reminders in the request? can you update your question.
            – Remul
            13 hours ago












          • That updated based on a relationship with the pet
            – Andre MacNamara
            13 hours ago













          up vote
          0
          down vote










          up vote
          0
          down vote









          You can use findOrNew, it will find the existing pet or instantiate a new instance.



          docs:



          foreach ($request->pets as $pet)
          {
          $clientPet = $client->pets()->findOrNew($pet['id']); // pass the pet id here
          $clientPet->name = $pet['name'];
          $clientPet->treated_at = $pet['treatedAt'];

          $clientPet->save();

          $date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
          foreach($clientPet->reminders as $reminders)
          {

          $reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
          $reminders->product_id = $pet['productId'];
          $reminders->application_id = $pet['applicationId'];

          $reminders->save();
          }
          }





          share|improve this answer














          You can use findOrNew, it will find the existing pet or instantiate a new instance.



          docs:



          foreach ($request->pets as $pet)
          {
          $clientPet = $client->pets()->findOrNew($pet['id']); // pass the pet id here
          $clientPet->name = $pet['name'];
          $clientPet->treated_at = $pet['treatedAt'];

          $clientPet->save();

          $date = Carbon::createFromFormat('Y-m-d', $clientPet->treated_at);
          foreach($clientPet->reminders as $reminders)
          {

          $reminders->scheduled_at = $date->addWeeks(12)->subDays(3);
          $reminders->product_id = $pet['productId'];
          $reminders->application_id = $pet['applicationId'];

          $reminders->save();
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 13 hours ago

























          answered 13 hours ago









          Remul

          58410




          58410












          • That creates the pets thanks. But it doesn't create the reminders (in the foreach loop). Any idea there?
            – Andre MacNamara
            13 hours ago










          • Are you passing the reminders with the request? At the moment your only getting them from the database.
            – Remul
            13 hours ago










          • So I have to create new ones and update them? How do I do that
            – Andre MacNamara
            13 hours ago










          • How are you passing the reminders in the request? can you update your question.
            – Remul
            13 hours ago












          • That updated based on a relationship with the pet
            – Andre MacNamara
            13 hours ago


















          • That creates the pets thanks. But it doesn't create the reminders (in the foreach loop). Any idea there?
            – Andre MacNamara
            13 hours ago










          • Are you passing the reminders with the request? At the moment your only getting them from the database.
            – Remul
            13 hours ago










          • So I have to create new ones and update them? How do I do that
            – Andre MacNamara
            13 hours ago










          • How are you passing the reminders in the request? can you update your question.
            – Remul
            13 hours ago












          • That updated based on a relationship with the pet
            – Andre MacNamara
            13 hours ago
















          That creates the pets thanks. But it doesn't create the reminders (in the foreach loop). Any idea there?
          – Andre MacNamara
          13 hours ago




          That creates the pets thanks. But it doesn't create the reminders (in the foreach loop). Any idea there?
          – Andre MacNamara
          13 hours ago












          Are you passing the reminders with the request? At the moment your only getting them from the database.
          – Remul
          13 hours ago




          Are you passing the reminders with the request? At the moment your only getting them from the database.
          – Remul
          13 hours ago












          So I have to create new ones and update them? How do I do that
          – Andre MacNamara
          13 hours ago




          So I have to create new ones and update them? How do I do that
          – Andre MacNamara
          13 hours ago












          How are you passing the reminders in the request? can you update your question.
          – Remul
          13 hours ago






          How are you passing the reminders in the request? can you update your question.
          – Remul
          13 hours ago














          That updated based on a relationship with the pet
          – Andre MacNamara
          13 hours ago




          That updated based on a relationship with the pet
          – Andre MacNamara
          13 hours ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265991%2fadding-a-new-record-on-a-update-field%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents