title: Can I use dynamic model on middleware?
body: <p>I have 2 routes that requires a person to be tagged to access the discussions.</p>
  <ol>
  <li><code>http://localhost:8000/api/fieldReports/{fieldReport}/discussions</code></li>
  <li><code>http://localhost:8000/api/agendas/{agenda}/discussions</code></li>
  </ol>
  <p>Currently, I have created this middleware, but instead of pointing right to a specific model, and duplicate it for each model with the exact same functionality, I want it to be more reusable.</p>
  <p><code>Middleware\ForbidUntaggedUser.php</code></p>
  <pre class="lang-php prettyprint-override"><code>class ForbidUntaggedUser
  {
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Closure  $next
       * @return mixed
       */
      public function handle(Request $request, Closure $next)
      {
          $user = $request-&gt;user();
          $report = $request-&gt;report; // &lt;-- I hardcoded the model, I want this to be dynamic
  
          // The `taggedUsers` remains the same (identical) for each model that has tagging system on it.
          if (!$report || !$report-&gt;taggedUsers-&gt;contains($user-&gt;id)) {
              return response()-&gt;json('Your action is unauthorized.', 403);
          }
  
          return $next($request);
      }
  }
  </code></pre>
  <p>I've tried to use Policy but it doesn't work, so I think I need a middleware for this.</p>
  <p><code>Policies\FieldReportDiscussionPolicy.php</code></p>
  <pre class="lang-php prettyprint-override"><code>class FieldReportDiscussionPolicy
  {
      use HandlesAuthorization;
  
      /**
       * Determine whether the user can view any models.
       *
       * @param  \App\Models\User  $user
       * @return mixed
       */
      public function viewAny(User $user, FieldReport $fieldReport)
      {
          return $user-&gt;can('view any fieldReportDiscussion')
              &amp;&amp; $fieldReport-&gt;taggedUsers-&gt;contains($user-&gt;id);
      }
  
      ... // and so on..
  }
  </code></pre>
  <p><code>Controllers\FieldReportDiscussionController.php</code></p>
  <pre class="lang-php prettyprint-override"><code>class FieldReportDiscussionController extends Controller
  {
      protected $model;
  
      /**
       * Create new instance.
       *
       * @return void
       */
      public function __construct()
      {
          $this-&gt;authorizeResource(
              FieldReportDiscussion::class,
              ['fieldReportDiscussion', 'fieldReport'] // This gave me error &quot;Array to string conversion&quot;
          );
  
          $this-&gt;model = new FieldReportDiscussion;
      }
  
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function index(FieldReport $fieldReport)
      {
          $discussions = $this-&gt;model-&gt;registries($fieldReport)-&gt;paginate(100);
          return response()-&gt;json($discussions);
      }
  }
  </code></pre>
  <p>I need the dependency injection on the controller because the route is nested with each model as the parent, like this one..</p>
  <p><code>routes\api.php</code></p>
  <pre class="lang-php prettyprint-override"><code>Route::apiResource('fieldReports', FieldReportController::class);
  Route::apiResource('fieldReports.discussions', FieldReportDiscussionController::class)-&gt;except(['update'])-&gt;parameter('discussions', 'fieldReportDiscussion');
  </code></pre>
  <p>So, what's the solution for this? Can I make it dynamic (the first request object)?</p>