Laravel function to add a supplier, and associated addresses and contacts











up vote
0
down vote

favorite












I have a function addSupplier() that adds a supplier. Each supplier has multiple addresses and each address has multiple contacts.



public function addSupplier ($request) {
// ADDING THE SUPPLIER BASIC INFO
$supplier_fillable_values = array_merge(
$request -> except('address_contact_inputs'),
['created_by' => $this->getAuthUserId()]
);
$new_supplier = Supplier::create($supplier_fillable_values);

// ADDING THE SUPPLIER ADDRESSES AND THE ADDRESS CONTACTS
foreach ($request -> address_contact_inputs as $address) {
// ADD ADDRESS
$new_supplier_address = $new_supplier -> addresses() -> create([
'address' => $address['address'],
'created_by' => $this -> getAuthUserId()
]);

// ADD ADDRESS CONTACTS
foreach ($address['contacts'] as $contact)
$new_supplier_address -> contacts() -> create([
'name' => $contact['name'],
'phone_number' => $contact['phone_number'],
'created_by' => $this -> getAuthUserId()
]);
}


return $request -> address_contact_inputs;
}


Everything works fine but question is: Is this the best practice or I should use events and create an event for:




  1. Storing the supplier basic info

  2. Storing the supplier addresses

  3. Storing contact for each supplier's address










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a function addSupplier() that adds a supplier. Each supplier has multiple addresses and each address has multiple contacts.



    public function addSupplier ($request) {
    // ADDING THE SUPPLIER BASIC INFO
    $supplier_fillable_values = array_merge(
    $request -> except('address_contact_inputs'),
    ['created_by' => $this->getAuthUserId()]
    );
    $new_supplier = Supplier::create($supplier_fillable_values);

    // ADDING THE SUPPLIER ADDRESSES AND THE ADDRESS CONTACTS
    foreach ($request -> address_contact_inputs as $address) {
    // ADD ADDRESS
    $new_supplier_address = $new_supplier -> addresses() -> create([
    'address' => $address['address'],
    'created_by' => $this -> getAuthUserId()
    ]);

    // ADD ADDRESS CONTACTS
    foreach ($address['contacts'] as $contact)
    $new_supplier_address -> contacts() -> create([
    'name' => $contact['name'],
    'phone_number' => $contact['phone_number'],
    'created_by' => $this -> getAuthUserId()
    ]);
    }


    return $request -> address_contact_inputs;
    }


    Everything works fine but question is: Is this the best practice or I should use events and create an event for:




    1. Storing the supplier basic info

    2. Storing the supplier addresses

    3. Storing contact for each supplier's address










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a function addSupplier() that adds a supplier. Each supplier has multiple addresses and each address has multiple contacts.



      public function addSupplier ($request) {
      // ADDING THE SUPPLIER BASIC INFO
      $supplier_fillable_values = array_merge(
      $request -> except('address_contact_inputs'),
      ['created_by' => $this->getAuthUserId()]
      );
      $new_supplier = Supplier::create($supplier_fillable_values);

      // ADDING THE SUPPLIER ADDRESSES AND THE ADDRESS CONTACTS
      foreach ($request -> address_contact_inputs as $address) {
      // ADD ADDRESS
      $new_supplier_address = $new_supplier -> addresses() -> create([
      'address' => $address['address'],
      'created_by' => $this -> getAuthUserId()
      ]);

      // ADD ADDRESS CONTACTS
      foreach ($address['contacts'] as $contact)
      $new_supplier_address -> contacts() -> create([
      'name' => $contact['name'],
      'phone_number' => $contact['phone_number'],
      'created_by' => $this -> getAuthUserId()
      ]);
      }


      return $request -> address_contact_inputs;
      }


      Everything works fine but question is: Is this the best practice or I should use events and create an event for:




      1. Storing the supplier basic info

      2. Storing the supplier addresses

      3. Storing contact for each supplier's address










      share|improve this question















      I have a function addSupplier() that adds a supplier. Each supplier has multiple addresses and each address has multiple contacts.



      public function addSupplier ($request) {
      // ADDING THE SUPPLIER BASIC INFO
      $supplier_fillable_values = array_merge(
      $request -> except('address_contact_inputs'),
      ['created_by' => $this->getAuthUserId()]
      );
      $new_supplier = Supplier::create($supplier_fillable_values);

      // ADDING THE SUPPLIER ADDRESSES AND THE ADDRESS CONTACTS
      foreach ($request -> address_contact_inputs as $address) {
      // ADD ADDRESS
      $new_supplier_address = $new_supplier -> addresses() -> create([
      'address' => $address['address'],
      'created_by' => $this -> getAuthUserId()
      ]);

      // ADD ADDRESS CONTACTS
      foreach ($address['contacts'] as $contact)
      $new_supplier_address -> contacts() -> create([
      'name' => $contact['name'],
      'phone_number' => $contact['phone_number'],
      'created_by' => $this -> getAuthUserId()
      ]);
      }


      return $request -> address_contact_inputs;
      }


      Everything works fine but question is: Is this the best practice or I should use events and create an event for:




      1. Storing the supplier basic info

      2. Storing the supplier addresses

      3. Storing contact for each supplier's address







      php mvc laravel repository






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 30 at 16:17









      200_success

      127k15148410




      127k15148410










      asked Oct 30 at 15:54









      Ahmed essam

      11




      11






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You could use events for triggering these kinds of things, but there is a danger that your code starts to become harder to follow. Martin Fowler gives a good top-level talk on it here.



          There are two issues jumping out at me:



          Firstly, your method accepts a request object so there is no indication as to what the requirements of that method are. You could change the method to accept specific arguments such as $created_by, $address_details, etc. This would make it easier to re-use the code elsewhere if needed.



          I would make the controller then responsible for preparing that set of data (e.g. handling the preparation of the address details array, getting the 'created_by' etc). And make your addSupplier method work with a set of arguments that could be run from anywhere in your app.



          public function addSupplier ($primary_contact_details, $contact_addresses, $created_by = NULL) {
          // ADDING THE SUPPLIER BASIC INFO
          $supplier_fillable_values = array_merge(
          $primary_contact_details,
          ['created_by' => $created_by]
          );
          $new_supplier = Supplier::create($supplier_fillable_values);

          // ADDING THE SUPPLIER ADDRESSES AND THE ADDRESS CONTACTS
          foreach ($contact_addresses as $address) {
          // ADD ADDRESS
          $new_supplier_address = $new_supplier -> addresses() -> create([
          'address' => $address['address'],
          'created_by' => $this -> getAuthUserId()
          ]);

          // ADD ADDRESS CONTACTS
          foreach ($address['contacts'] as $contact)
          $new_supplier_address -> contacts() -> create([
          'name' => $contact['name'],
          'phone_number' => $contact['phone_number'],
          'created_by' => $this -> getAuthUserId()
          ]);
          }

          return $request -> address_contact_inputs;
          }


          Secondly, it's good practise to avoid too many levels of indention, so you could refactor to use one level of indentation and splitting out the foreach loops to their own public/private methods on the same class.






          share|improve this answer










          New contributor




          benjaminhull is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            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: "196"
            };
            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: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2fcodereview.stackexchange.com%2fquestions%2f206588%2flaravel-function-to-add-a-supplier-and-associated-addresses-and-contacts%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            You could use events for triggering these kinds of things, but there is a danger that your code starts to become harder to follow. Martin Fowler gives a good top-level talk on it here.



            There are two issues jumping out at me:



            Firstly, your method accepts a request object so there is no indication as to what the requirements of that method are. You could change the method to accept specific arguments such as $created_by, $address_details, etc. This would make it easier to re-use the code elsewhere if needed.



            I would make the controller then responsible for preparing that set of data (e.g. handling the preparation of the address details array, getting the 'created_by' etc). And make your addSupplier method work with a set of arguments that could be run from anywhere in your app.



            public function addSupplier ($primary_contact_details, $contact_addresses, $created_by = NULL) {
            // ADDING THE SUPPLIER BASIC INFO
            $supplier_fillable_values = array_merge(
            $primary_contact_details,
            ['created_by' => $created_by]
            );
            $new_supplier = Supplier::create($supplier_fillable_values);

            // ADDING THE SUPPLIER ADDRESSES AND THE ADDRESS CONTACTS
            foreach ($contact_addresses as $address) {
            // ADD ADDRESS
            $new_supplier_address = $new_supplier -> addresses() -> create([
            'address' => $address['address'],
            'created_by' => $this -> getAuthUserId()
            ]);

            // ADD ADDRESS CONTACTS
            foreach ($address['contacts'] as $contact)
            $new_supplier_address -> contacts() -> create([
            'name' => $contact['name'],
            'phone_number' => $contact['phone_number'],
            'created_by' => $this -> getAuthUserId()
            ]);
            }

            return $request -> address_contact_inputs;
            }


            Secondly, it's good practise to avoid too many levels of indention, so you could refactor to use one level of indentation and splitting out the foreach loops to their own public/private methods on the same class.






            share|improve this answer










            New contributor




            benjaminhull is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






















              up vote
              0
              down vote













              You could use events for triggering these kinds of things, but there is a danger that your code starts to become harder to follow. Martin Fowler gives a good top-level talk on it here.



              There are two issues jumping out at me:



              Firstly, your method accepts a request object so there is no indication as to what the requirements of that method are. You could change the method to accept specific arguments such as $created_by, $address_details, etc. This would make it easier to re-use the code elsewhere if needed.



              I would make the controller then responsible for preparing that set of data (e.g. handling the preparation of the address details array, getting the 'created_by' etc). And make your addSupplier method work with a set of arguments that could be run from anywhere in your app.



              public function addSupplier ($primary_contact_details, $contact_addresses, $created_by = NULL) {
              // ADDING THE SUPPLIER BASIC INFO
              $supplier_fillable_values = array_merge(
              $primary_contact_details,
              ['created_by' => $created_by]
              );
              $new_supplier = Supplier::create($supplier_fillable_values);

              // ADDING THE SUPPLIER ADDRESSES AND THE ADDRESS CONTACTS
              foreach ($contact_addresses as $address) {
              // ADD ADDRESS
              $new_supplier_address = $new_supplier -> addresses() -> create([
              'address' => $address['address'],
              'created_by' => $this -> getAuthUserId()
              ]);

              // ADD ADDRESS CONTACTS
              foreach ($address['contacts'] as $contact)
              $new_supplier_address -> contacts() -> create([
              'name' => $contact['name'],
              'phone_number' => $contact['phone_number'],
              'created_by' => $this -> getAuthUserId()
              ]);
              }

              return $request -> address_contact_inputs;
              }


              Secondly, it's good practise to avoid too many levels of indention, so you could refactor to use one level of indentation and splitting out the foreach loops to their own public/private methods on the same class.






              share|improve this answer










              New contributor




              benjaminhull is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.




















                up vote
                0
                down vote










                up vote
                0
                down vote









                You could use events for triggering these kinds of things, but there is a danger that your code starts to become harder to follow. Martin Fowler gives a good top-level talk on it here.



                There are two issues jumping out at me:



                Firstly, your method accepts a request object so there is no indication as to what the requirements of that method are. You could change the method to accept specific arguments such as $created_by, $address_details, etc. This would make it easier to re-use the code elsewhere if needed.



                I would make the controller then responsible for preparing that set of data (e.g. handling the preparation of the address details array, getting the 'created_by' etc). And make your addSupplier method work with a set of arguments that could be run from anywhere in your app.



                public function addSupplier ($primary_contact_details, $contact_addresses, $created_by = NULL) {
                // ADDING THE SUPPLIER BASIC INFO
                $supplier_fillable_values = array_merge(
                $primary_contact_details,
                ['created_by' => $created_by]
                );
                $new_supplier = Supplier::create($supplier_fillable_values);

                // ADDING THE SUPPLIER ADDRESSES AND THE ADDRESS CONTACTS
                foreach ($contact_addresses as $address) {
                // ADD ADDRESS
                $new_supplier_address = $new_supplier -> addresses() -> create([
                'address' => $address['address'],
                'created_by' => $this -> getAuthUserId()
                ]);

                // ADD ADDRESS CONTACTS
                foreach ($address['contacts'] as $contact)
                $new_supplier_address -> contacts() -> create([
                'name' => $contact['name'],
                'phone_number' => $contact['phone_number'],
                'created_by' => $this -> getAuthUserId()
                ]);
                }

                return $request -> address_contact_inputs;
                }


                Secondly, it's good practise to avoid too many levels of indention, so you could refactor to use one level of indentation and splitting out the foreach loops to their own public/private methods on the same class.






                share|improve this answer










                New contributor




                benjaminhull is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                You could use events for triggering these kinds of things, but there is a danger that your code starts to become harder to follow. Martin Fowler gives a good top-level talk on it here.



                There are two issues jumping out at me:



                Firstly, your method accepts a request object so there is no indication as to what the requirements of that method are. You could change the method to accept specific arguments such as $created_by, $address_details, etc. This would make it easier to re-use the code elsewhere if needed.



                I would make the controller then responsible for preparing that set of data (e.g. handling the preparation of the address details array, getting the 'created_by' etc). And make your addSupplier method work with a set of arguments that could be run from anywhere in your app.



                public function addSupplier ($primary_contact_details, $contact_addresses, $created_by = NULL) {
                // ADDING THE SUPPLIER BASIC INFO
                $supplier_fillable_values = array_merge(
                $primary_contact_details,
                ['created_by' => $created_by]
                );
                $new_supplier = Supplier::create($supplier_fillable_values);

                // ADDING THE SUPPLIER ADDRESSES AND THE ADDRESS CONTACTS
                foreach ($contact_addresses as $address) {
                // ADD ADDRESS
                $new_supplier_address = $new_supplier -> addresses() -> create([
                'address' => $address['address'],
                'created_by' => $this -> getAuthUserId()
                ]);

                // ADD ADDRESS CONTACTS
                foreach ($address['contacts'] as $contact)
                $new_supplier_address -> contacts() -> create([
                'name' => $contact['name'],
                'phone_number' => $contact['phone_number'],
                'created_by' => $this -> getAuthUserId()
                ]);
                }

                return $request -> address_contact_inputs;
                }


                Secondly, it's good practise to avoid too many levels of indention, so you could refactor to use one level of indentation and splitting out the foreach loops to their own public/private methods on the same class.







                share|improve this answer










                New contributor




                benjaminhull is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer








                edited 2 days ago





















                New contributor




                benjaminhull is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered 2 days ago









                benjaminhull

                61




                61




                New contributor




                benjaminhull is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                benjaminhull is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                benjaminhull is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206588%2flaravel-function-to-add-a-supplier-and-associated-addresses-and-contacts%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







                    Popular posts from this blog

                    Список кардиналов, возведённых папой римским Каликстом III

                    Deduzione

                    Mysql.sock missing - “Can't connect to local MySQL server through socket”