title: Extending Eloquent Models with dynamic global scopes
body: <p>I have a MySQL table that receives many different Jotform reports in JSON payloads. This has helped tremendously in capturing queryable data quickly without adding to the front-end developer's workload.</p>
  <p>I created an eloquent model for the table. I now would like to be able to create models that extend it for each Jotform we create. I feel like it will increase the readability of my code drastically.</p>
  <p>My eloquent model is called <code>RawDataReport</code>. It has <code>created_at</code>, <code>updated_at</code>, <code>data</code>, and <code>report name</code> columns in the table. I want to create the model <code>ShiftInspectionReport</code> extending the <code>RawDataReport</code>.</p>
  <p>I have two JotForm reports one is called <code>Shift Inspection Report</code> and one is called <code>Need Inspection Report</code>. Both are part of the <code>ShiftInspectionReport</code> model.</p>
  <p>So I need to query the <code>RawDataReports</code> table for any reports matching those names. I frequently need to query the <code>RawDataReports</code> <code>report_name</code> column with either one or more report names.</p>
  <p>To help with this I created a local scope to query the report name which accepts either a string report name or an array of string report names.  Here is the local scope on the <code>RawDataReports</code> model.</p>
  <pre class="lang-php prettyprint-override"><code>protected function scopeReportName($query, $report_name): \Illuminate\Database\Eloquent\Builder
  {
      if (is_array($report_name)) {
          return $query-&gt;orWhere(function ($query) USE ($report_name) {
              ForEach($report_name as $report) {
                  if (is_string($report) === false) {
                      throw new \Exception('$report_name must be an array of strings or a string');
                  }
      
                  $query-&gt;where('report_name', $report);
              }
          });
      } else {
          if (is_string($report_name) === false) {
              throw new \Exception('$report_name must be an array of strings or a string');
          }
  
          return $query-&gt;where('report_name', $report_name);
      }
  }
  </code></pre>
  <p>EDIT - after comments I simplified the reportName scope</p>
  <pre><code>    protected function scopeReportName($query,array $report_name): \Illuminate\Database\Eloquent\Builder
      {
          return $query-&gt;whereIn('report_name',$report_name);
      }
  </code></pre>
  <p>Now in my <code>ShiftInspectionReport</code> model, I'd like to add a global scope that can use that local scope and pass in the $report_name.  But according to this article, <a href="https://stackoverflow.com/questions/39277768/laravel-5-global-scope-with-dynamic-parameter">Laravel 5 Global Scope with Dynamic Parameter</a>, it doesn't look like Laravel global scopes allow you to use dynamic variables.</p>
  <p>I could just create a local scope in <code>ShiftInspectionReport</code> but the readability would look like</p>
  <pre class="lang-php prettyprint-override"><code>$reports = ShiftInspectionReport::shiftInspectionReport()-&gt;startDate('2021-05-15')-&gt;get()
  </code></pre>
  <p>when I'd really like to be able to just call</p>
  <pre class="lang-php prettyprint-override"><code>ShiftInspectionReport::startDate('2021-05-15')-&gt;get()
  </code></pre>
  <p>Any suggestions or comments would be appreciated.</p>
  <p>Thank you</p>