Laravel ignore unique validation on update









up vote
0
down vote

favorite












I have a Model which contains many foreign keys. One of those foreign keys must be a unique value.



My validation rules are the following ones:



$data['rules'] = [
'address' => 'required|string',
'buyer_id' => 'required|exists:buyers,id',
'buyer_name' => 'required|string',
'date' => 'required|date',
'email' => 'required|email',
'identification_photo' => 'required|string',
'invoice' => 'string|required',
'middleman_id' => 'nullable|exists:middlemen,id',
'price' => 'required|numeric|between:1,99999999999999999999999999.9999',
'property_id' => 'required|exists:properties,id|unique:reservations,property_id',
'purchase_receipt' => 'required|string',
'rfc' => array(
'required',
'regex:/^([A-Z,Ñ,&]3,4([0-9]2)(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])[A-Z|d]3)$/'
),
'tier_id' => 'nullable|exists:tiers,id',
'user_id' => 'required|exists:users,id',
];


The one that I have trouble is property_id. This must be unique in the current table which is reservations.



In order to ignore that validation when I make an update, I'm adding this line of code before calling the Validator:
$book['rules']['property_id'] .= ",$item->property_id";



And when I do a Log::info of all my rules, I got the following line: 'property_id' => 'required|exists:properties,id|unique:reservations,property_id,4',



But I keep receiving the error. Am I doing something wrong?










share|improve this question

























    up vote
    0
    down vote

    favorite












    I have a Model which contains many foreign keys. One of those foreign keys must be a unique value.



    My validation rules are the following ones:



    $data['rules'] = [
    'address' => 'required|string',
    'buyer_id' => 'required|exists:buyers,id',
    'buyer_name' => 'required|string',
    'date' => 'required|date',
    'email' => 'required|email',
    'identification_photo' => 'required|string',
    'invoice' => 'string|required',
    'middleman_id' => 'nullable|exists:middlemen,id',
    'price' => 'required|numeric|between:1,99999999999999999999999999.9999',
    'property_id' => 'required|exists:properties,id|unique:reservations,property_id',
    'purchase_receipt' => 'required|string',
    'rfc' => array(
    'required',
    'regex:/^([A-Z,Ñ,&]3,4([0-9]2)(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])[A-Z|d]3)$/'
    ),
    'tier_id' => 'nullable|exists:tiers,id',
    'user_id' => 'required|exists:users,id',
    ];


    The one that I have trouble is property_id. This must be unique in the current table which is reservations.



    In order to ignore that validation when I make an update, I'm adding this line of code before calling the Validator:
    $book['rules']['property_id'] .= ",$item->property_id";



    And when I do a Log::info of all my rules, I got the following line: 'property_id' => 'required|exists:properties,id|unique:reservations,property_id,4',



    But I keep receiving the error. Am I doing something wrong?










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a Model which contains many foreign keys. One of those foreign keys must be a unique value.



      My validation rules are the following ones:



      $data['rules'] = [
      'address' => 'required|string',
      'buyer_id' => 'required|exists:buyers,id',
      'buyer_name' => 'required|string',
      'date' => 'required|date',
      'email' => 'required|email',
      'identification_photo' => 'required|string',
      'invoice' => 'string|required',
      'middleman_id' => 'nullable|exists:middlemen,id',
      'price' => 'required|numeric|between:1,99999999999999999999999999.9999',
      'property_id' => 'required|exists:properties,id|unique:reservations,property_id',
      'purchase_receipt' => 'required|string',
      'rfc' => array(
      'required',
      'regex:/^([A-Z,Ñ,&]3,4([0-9]2)(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])[A-Z|d]3)$/'
      ),
      'tier_id' => 'nullable|exists:tiers,id',
      'user_id' => 'required|exists:users,id',
      ];


      The one that I have trouble is property_id. This must be unique in the current table which is reservations.



      In order to ignore that validation when I make an update, I'm adding this line of code before calling the Validator:
      $book['rules']['property_id'] .= ",$item->property_id";



      And when I do a Log::info of all my rules, I got the following line: 'property_id' => 'required|exists:properties,id|unique:reservations,property_id,4',



      But I keep receiving the error. Am I doing something wrong?










      share|improve this question













      I have a Model which contains many foreign keys. One of those foreign keys must be a unique value.



      My validation rules are the following ones:



      $data['rules'] = [
      'address' => 'required|string',
      'buyer_id' => 'required|exists:buyers,id',
      'buyer_name' => 'required|string',
      'date' => 'required|date',
      'email' => 'required|email',
      'identification_photo' => 'required|string',
      'invoice' => 'string|required',
      'middleman_id' => 'nullable|exists:middlemen,id',
      'price' => 'required|numeric|between:1,99999999999999999999999999.9999',
      'property_id' => 'required|exists:properties,id|unique:reservations,property_id',
      'purchase_receipt' => 'required|string',
      'rfc' => array(
      'required',
      'regex:/^([A-Z,Ñ,&]3,4([0-9]2)(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])[A-Z|d]3)$/'
      ),
      'tier_id' => 'nullable|exists:tiers,id',
      'user_id' => 'required|exists:users,id',
      ];


      The one that I have trouble is property_id. This must be unique in the current table which is reservations.



      In order to ignore that validation when I make an update, I'm adding this line of code before calling the Validator:
      $book['rules']['property_id'] .= ",$item->property_id";



      And when I do a Log::info of all my rules, I got the following line: 'property_id' => 'required|exists:properties,id|unique:reservations,property_id,4',



      But I keep receiving the error. Am I doing something wrong?







      laravel laravel-5 eloquent






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 4 at 4:16









      Jacobo Tapia

      153111




      153111






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The error is on this line:



          $book['rules']['property_id'] .= ",$item->property_id";


          Instead of passing the id of the foreign key you want to ignore, you have to give the current model ID in order to ignore that validation for a specific item.



          $book['rules']['property_id'] .= ",$item->id";


          With this you tell that for your Model with id = x, ignore that specific validation. To understand it in a better way, you are telling that for this validation, ignore the validation of the property only for the record with id equals to the $item->id.






          share|improve this answer





























            up vote
            0
            down vote













            I think the line of code you are looking for is



            'property_id' => 'required|exists:properties,id|unique:reservations,property_id,'.$request->id',



            This ignores the current row you are updating while also validating your property_id






            share|improve this answer




















              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%2f53137698%2flaravel-ignore-unique-validation-on-update%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              The error is on this line:



              $book['rules']['property_id'] .= ",$item->property_id";


              Instead of passing the id of the foreign key you want to ignore, you have to give the current model ID in order to ignore that validation for a specific item.



              $book['rules']['property_id'] .= ",$item->id";


              With this you tell that for your Model with id = x, ignore that specific validation. To understand it in a better way, you are telling that for this validation, ignore the validation of the property only for the record with id equals to the $item->id.






              share|improve this answer


























                up vote
                1
                down vote



                accepted










                The error is on this line:



                $book['rules']['property_id'] .= ",$item->property_id";


                Instead of passing the id of the foreign key you want to ignore, you have to give the current model ID in order to ignore that validation for a specific item.



                $book['rules']['property_id'] .= ",$item->id";


                With this you tell that for your Model with id = x, ignore that specific validation. To understand it in a better way, you are telling that for this validation, ignore the validation of the property only for the record with id equals to the $item->id.






                share|improve this answer
























                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  The error is on this line:



                  $book['rules']['property_id'] .= ",$item->property_id";


                  Instead of passing the id of the foreign key you want to ignore, you have to give the current model ID in order to ignore that validation for a specific item.



                  $book['rules']['property_id'] .= ",$item->id";


                  With this you tell that for your Model with id = x, ignore that specific validation. To understand it in a better way, you are telling that for this validation, ignore the validation of the property only for the record with id equals to the $item->id.






                  share|improve this answer














                  The error is on this line:



                  $book['rules']['property_id'] .= ",$item->property_id";


                  Instead of passing the id of the foreign key you want to ignore, you have to give the current model ID in order to ignore that validation for a specific item.



                  $book['rules']['property_id'] .= ",$item->id";


                  With this you tell that for your Model with id = x, ignore that specific validation. To understand it in a better way, you are telling that for this validation, ignore the validation of the property only for the record with id equals to the $item->id.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 at 5:43

























                  answered Nov 4 at 4:29









                  Jacobo Tapia

                  153111




                  153111






















                      up vote
                      0
                      down vote













                      I think the line of code you are looking for is



                      'property_id' => 'required|exists:properties,id|unique:reservations,property_id,'.$request->id',



                      This ignores the current row you are updating while also validating your property_id






                      share|improve this answer
























                        up vote
                        0
                        down vote













                        I think the line of code you are looking for is



                        'property_id' => 'required|exists:properties,id|unique:reservations,property_id,'.$request->id',



                        This ignores the current row you are updating while also validating your property_id






                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          I think the line of code you are looking for is



                          'property_id' => 'required|exists:properties,id|unique:reservations,property_id,'.$request->id',



                          This ignores the current row you are updating while also validating your property_id






                          share|improve this answer












                          I think the line of code you are looking for is



                          'property_id' => 'required|exists:properties,id|unique:reservations,property_id,'.$request->id',



                          This ignores the current row you are updating while also validating your property_id







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 4 at 5:10









                          Bonish Koirala

                          3938




                          3938



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53137698%2flaravel-ignore-unique-validation-on-update%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Barbados

                              How to read a connectionString WITH PROVIDER in .NET Core?

                              Node.js Script on GitHub Pages or Amazon S3