title: Create dynamic request object in Laravel
body: <p>I was looking for help creating an object of the type Illuminate\Http\Request. This article helped me understand the mechanism of the class, but I did not get the desired result.</p>
  <p><a href="https://stackoverflow.com/questions/40262947/create-a-laravel-request-object-on-the-fly">Create a Laravel Request object on the fly</a></p>
  <p>I'm editing the development code passed to me by the customer. The code has a function that gets a request parameter from vue and translates it to JSON:</p>
  <pre><code>$json = $request-&gt;json()-&gt;get('data');
  $json['some_key'];
  </code></pre>
  <p>This code returned an empty array of data:</p>
  <pre><code>$json = $request-&gt;request-&gt;add([some data]);
  </code></pre>
  <p>or</p>
  <pre><code>$json = $request-&gt;request-&gt;replace([some data]);
  </code></pre>
  <p>this one returned an error for missing the add parameter</p>
  <pre><code>$json = $request-&gt;json-&gt;replace([some data]);
  </code></pre>
  <p>A variant was found by trying and errors. Maybe, it help someone save time:</p>
  <pre><code>public function index() {
    $myRequest = new \Illuminate\Http\Request();
    $myRequest-&gt;json()-&gt;replace(['data' =&gt; ['some_key' =&gt; $some_data]]);
    $data = MyClass::getData($myRequest);
  }
  
  ..
  class MyClass extends ...
  {
    public static function getData(Request $request) {
       $json = $request-&gt;json()-&gt;get('data');
       $json['some_key'];
  </code></pre>
  <p>In addition, there are other fields in the class that you can also slip data into so that you can pass everything you want via Request</p>
  <pre><code>$myRequest-&gt;json()-&gt;replace(['data' =&gt; ['some_key' =&gt; $some_data]]);
  ..        
  $myRequest-&gt;request-&gt;replace(['data' =&gt; ['some_key' =&gt; $some_data]]);
  ..
  $myRequest-&gt;attributes-&gt;replace(['data' =&gt; ['some_key' =&gt; $some_data]]);
  ..
  $myRequest-&gt;query-&gt;replace(['data' =&gt; ['some_key' =&gt; $some_data]]);
  </code></pre>