0
votes

I am very new to PHP & HTTP, and have come across an issue that seems a bit odd while trying to debug a function:

There is a page displaying a list of transactions (both historic & pending). The information is displayed in a table, with a row for each transaction. Each transaction displays information such as Customer Name, ID, Amount, Transaction Contact, Provisional Contacts, Status, etc

The user can update the transaction contact by selecting a name from a pre-populated drop down list in the Transaction Contact column. If they want to add a new name to that drop down list, they can click an Edit button in the cell in the Provisional Contacts list, which will open a dialog displaying a form with fields for 'First Name', 'Last Name' & 'Email Address'. Once these details have been added, they can click an 'Add User' button on the form, and the new user will be added to the list of contacts in that cell. All of the users in this cell will be displayed in the drop down in the Transaction Contact cell for that transaction (along with some other default contacts).

However, there is currently an issue where, when the user adds a new contact to the Provisional Contacts cell, although that user is then displayed in the Transaction Contact drop down, when you select the new user, an error is displayed stating that the user doesn't exist. However, if you refresh the page after adding the contact to the Provisional Contacts cell, and then try to select it from the Transaction Contact drop down, it is possible to select it.

I've narrowed down the issue to being somewhere in the addAccountUser() function defined in UserController.php:

public function addAccountUser( AddAccountUsersRequest $request )
{
  dd('addAccountUser() being called ');
  //dd($request);
  $users = $request->input('users');
  $type = $request->input('type');
  $accountId = $request->input('accountId');
  dd('Value of users: ');
  dd($type);

      // If adding an agent, the new user should be agent, else the created user will be direct
      $userType = $type == 'taxfirm-agent' ? UserType::where('userTypeTag', 'AGENT')->first() : UserType::where('userTypeTag', 'DIRECT')->first();
      $messages = array();
      $hasWarningMessages = false;

      try
      {
          DB::beginTransaction();

        foreach ($users as $userRaw)
        {

              $details = array(
                  'firstName' => $userRaw['firstName'],
                  'lastName'  => $userRaw['lastName'],
                  'email'     => $userRaw['email'],
                  'password'  => uniqid(),
                  'userTypeId' => $userType->userTypeId,
                  'accountId' => (!empty($accountId)) ? $accountId : null
              );
        $propertyValues = array();

        // Adding tax agent
        if ($type == 'taxfirm-agent') {
                  $group = $userRaw['role'];
          $rv = $this->addTaxfirmAgent($details, $group);
        }
        else if($type == 'taxfirm-direct') {
          $rv = $this->addTaxfirmDirectContact($details);
        }
        else {
                  $group = $userRaw['role'];
          $rv = $this->addTaxpayerDirectContact($details, $group);
        }

            DB::commit();

        if ($rv['status'] !== 'SUCCESS') {
            if (!isset($messages[$rv['status']])) {
              $messages[$rv['status']] = array(
                'message' => StatusMessage::getMessage($rv['status']),
                'data' => [],
              );
            }

            $messages[$rv['status']]['data'][] = [$userRaw['email'], ucfirst($userRaw['firstName']), ucfirst($userRaw['lastName'])];

            if (!$hasWarningMessages)
            {
              $hasWarningMessages = true;
            }
        }
        }
    }
      catch(\Exception $e)
      {
          DB::rollback();
    return response()->json(array(
      'success' => false,
        'exceptionCode' => $e->getCode(),
        'exceptionMessage' => $e->getMessage().' - '.$e->getFile().' - '.$e->getLine()
    ), 400);
      }

      // When returning $messages to angular, the array is turned into an object
      // which is not desirable. This is a workaround so that the messages are returned
      // as an array
      $outputMsg = array();
      foreach ($messages as $value) {
          $outputMsg[] = $value;
      }

  return response()->json(array(
    'success' => true,
    'hasWarningMessages' => $hasWarningMessages,
          'result' => $outputMsg,
          'users' => $rv['user']->user,
  ));
}

Currently, when I try adding a new user to the Provisional Contact cell for a transaction, an error is displayed in the browser which says:

An error has occurred adding your user. If the problem persists, please contact us.

I can see the addAccountUser() being called debug from the start of the addAccountUser() function in the Network -> Preview tab of my browser console at this point, but can't see the Value of users: or $type debug that I've added, which suggests that something is going wrong in one of the following three lines:

$users = $request->input('users');
$type = $request->input('type');
$accountId = $request->input('accountId');

I don't fully understand what's going on in these three lines... I know that they are HTTP requests, but don't really know what information they are requesting, or from where...

Can anyone point me in the right direction to work this out?

1
To start with, remove all those dd()-calls, since (if you're using Laravel) those function calls will dump the value and then totally kill the execution of the script. - Magnus Eriksson
Ah OK. Yes, I am using Laravel- how do I debug it without killing the script? - Noble-Surfer
I'm used to using JavaScript console.log() statements to debug stuff in the web console, but don't know the equivalent for PHP... - Noble-Surfer
So, when I remove the dd() calls, the user is successfully added, and I can see it in the drop down for the Transaction Contact, but when I try to select it, I get a message that says Unexpected token < in JSON at position 0 in the browser window (i.e. pops up just underneath the address bar- it's not a message in the console). - Noble-Surfer

1 Answers

1
votes

When you call a dd() function, the script will output anything given to the function and quit afterwards. So, it wont execute any of the following code.

So, things like:

dd('Value of users: ');
dd($type);

Will never work, because the second dd() will never get executed because the script quit after the first dd().

PHP is server side, so you must render something on the backend and display it in the browser. PHP can't write to the browser console. How i would go about debugging this, is removing the first dd() call (dd('addAccountUser() being called ');) and see if the following dd() call can be reached.

If this is a post request in your browser, you can inspect the request in the Developer console (F12) and see what the response is from the backend. If it is something like Value of users: then you know the second dd() call can be reached.

Also, keeping the dd() in your controller will always return an error code to the front end.