title: Change request input values from empty string to null in Laravel
body: <p>In my controller I receive an <code>Illuminate\Http\Request</code> Request that contains some data with values equal to an empty string:</p>
  
  <pre><code>dd($request-&gt;input())
  
  //output
  array:5 [▼
    "_token" =&gt; "K43q88mR5zQRqVPAuYX5IWtQ1khQ24JTsuxl8mz4"
    "param1" =&gt; "1"
    "param2" =&gt; "2"
    "param3" =&gt; ""
    "param4" =&gt; ""
  ]
  </code></pre>
  
  <p>(<em>basically that happens when the user had made no selections in the create form BUT due to frontend restrictions <a href="https://github.com/select2/select2/issues/841" rel="noreferrer">see here</a>, <strong>I am not able to change the form of the Request</strong> so that to exclude the empty string values from being sent to the server</em>)</p>
  
  <p>The request input data is used to attach a relationships to models. For example:</p>
  
  <pre><code>$book-&gt;authors()-&gt;attach($request-&gt;input('param1'));
  </code></pre>
  
  <p>The problem is that when the input value is equal to the empty string <code>""</code> the above line  throws a <code>QueryException</code>, since it tries to add a foreign key equal to <code>""</code> to the pivot table. </p>
  
  <p>On the other hand, I noticed that if the value is <code>null</code> instead of <code>""</code>, the <code>attach()</code> method is not executed and, thus, does not throws an Exception, neither updates the database, which is exactly the behavior I want, when the user made no selection in the input form. </p>
  
  <p>My question is <strong>how can I change the request values from <code>""</code> to <code>null</code>?</strong> My first thought was to make a helper function that iterates over the request input array and replace "" with null and use it in the controller BUT it does not seems like a good idea for me, because this would not prevent possible unwanted validation errors, when using Form Request Validation.</p>
  
  <p>Any ideas please ???</p>
  
  <p>Thank you in advance</p>