title: Calling a method in the middle of a another one prevents following variables from being set - Laravel job
body: <p>I have a Laravel Job which sets some data and then uses the data to create an entry in the database. All the fields in the database table are <code>NULL</code>able. There's the <code>custom_variables</code> field - set using a custom method <code>getByPrefix()</code>.</p>
  
  <p><strong>MyJob.php</strong></p>
  
  <pre><code>&lt;?php
  
  class MyJob implements ShouldQueue {
  
      public function __construct($input) {
          $this-&gt;input = $input;
      }
  
      public function handle() {
  
          $data = $this-&gt;getData();
  
          MyModel::create($data);
  
      }
  
      protected function getData() {
  
          if (isset($this-&gt;input['name'])) {
              $data['name'] = $this-&gt;input['name'];
          }
  
  
          $data['custom_variables'] = $this-&gt;getByPrefix('custom-');
  
          if (isset($this-&gt;input['surname'])) {
              $data['surname'] = $this-&gt;input['surname'];
          }
  
          return $data;
      }
  
  
      /**
       * Filter the input by the provided prefix 
       * and return matching input data.
       * @return null|string
       */
      protected function getByPrefix($prefix) {
  
          $this-&gt;input= array_filter($this-&gt;input, function($k) use ($prefix) {
              return strpos($k, $prefix) !== false;
          }, ARRAY_FILTER_USE_KEY);
  
          if (count($this-&gt;input) === 0) {
              return null;
          }
  
          $data = array();
  
          foreach ($this-&gt;inputas $k =&gt; $v) {
              array_push($data, array($k =&gt; $v));
          }
  
          if (empty($data)) {
              return null;
          }
  
          return json_encode($data);
  
      }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>The problem</strong> is that if I keep the call to <code>getByPrefix()</code> in the middle, then the value of <code>$data['surname']</code> is always <code>NULL</code> when the record is stored, even if the <code>surname</code> is present in the input.</p>
  
  <p>When I move the call to <code>getByPrefix()</code> to the end of the script, the <code>$data['surname']</code> is set properly.</p>
  
  <p><strong>Why is this happening?</strong> Is it because I <em>might</em> return JSON from the <code>getByPrefix()</code> method? Don't think so, but who knows.</p>
  
  <p>I tried wrapping the body of <code>getByPrefix()</code> in a <code>try-catch</code> - but there are no errors whatsoever, and the <code>custom_variables</code> field is always set in the DB.</p>
  
  <p>Any idea what might be happening here?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Example input data:</p>
  
  <pre><code>array(
      'name' =&gt; 'John', 
      'surname' =&gt; 'Doe', 
      'custom-var' =&gt; 'customValue'
  )
  </code></pre>
  
  <p>Output (after <code>getByPrefix()</code> is applied):</p>
  
  <pre><code>array(
      'name' =&gt; 'John', 
      'custom_variables' =&gt; "[{"custom-var":"customValue"}]"
  )
  </code></pre>