title: Lumen - How to create a request object that can be accessed by app('request') from a different file?
body: <p>My lumen application has a common trait that records every add/update/delete activity of every table in the database.  This trait is used in a basemodel which is then extended for all applicable table model files like below.</p>
  
  <pre><code>namespace App\Models;
  class BaseModel extends Model {
     use RecordsActivity; //this is the common trait
  }
  
  namespace App\Models;
  class User extends BaseModel {}
  </code></pre>
  
  <p>Till date, my application only had APIs which will be consumed by the Angular client.  So, every API endpoint comes with a request in which I'll store a property in the middleware as end_point_id (dynamic for every API call).  This end_point_id will be stored as part of recording add/update/delete transaction to relate the transactions with the endpoint transaction like below.</p>
  
  <pre><code>namespace App\Http\Middleware;
  class APIMiddleware {
     //check and do some process
     $request-&gt;merge(['end_point_id' =&gt; $end_point_id]);
  }
  
  namespace App\Http\Traits;
  trait RecordsActivity {
     //other preparation statements
     AuditTrail::create([
        "end_point_id" =&gt; app('request')-&gt;end_point_id,
        'table_name' =&gt; $model-&gt;getTable(),
        'object_id' =&gt; $model-&gt;id,
        'field_name' =&gt; $field_name,
        'old_value' =&gt; $old_value,
        'new_value' =&gt; $new_value,
        'event_timestamp' =&gt; $model-&gt;updated_at]);
  }
  </code></pre>
  
  <p>By referring app('request'), the trait was getting the value of end_point_id when consuming as an API.  Now I'm writing a job to perform some batch process on a few tables commonly used by API.  Now I've renamed end_point_id as 'event_id' and added a new column as 'event_trigger' to hold 'API/JOB'.  When a job is executed, the table add/update/delete transactions will be saved in audit_trails table with event_trigger as 'JOB' and event_id as an integer job_id. </p>
  
  <p>Since the job does not have an input request, I tried creating an Illuminate\Http\Request object and included a property named 'event_id' using $request->replace (also tried merge method).  By doing so, the newly included property is not accessible when tried as app('request')->event_id.  Below is the printed log info.</p>
  
  <pre><code>TestJob.php 
  namespace App\Console\Commands;
  class TestJob extends Command {
  
     public function handle() {
       $request = new \Illuminate\Http\Request;
       $request-&gt;replace(['event_id' =&gt; 11]);
       Log::info(print_r($request-&gt;all(), true));
       //prints output as Array
       //(
       //     [event_id] =&gt; 11
       //)
       Log::info(print_r(app('request')-&gt;all(),true));
       //prints output as Array
       //(
       //)
      }
  }
  </code></pre>
  
  <p>Because <code>app('request')-&gt;all()</code> prints empty array, during any table update, the trait fails as the value of event_id is not present. </p>
  
  <p>I tried creating <code>$request = new \Request()</code>, it failed as</p>
  
  <blockquote>
    <p>"call to undefined method replace()";</p>
  </blockquote>
  
  <p>same for <code>merge()</code> too.  Same error when trying to create a Guzzle request.</p>
  
  <p>Since lumen is stateless, passing a value from one component to another through request object was the only option that came to my mind.  Any help to resolve the issue is appreciated.  I would like to know how to create a request object from a job that will be accessible using app('request') command from another file.</p>
  
  <p>Thank you!</p>