I was under the impression that middleware runs BEFORE the route functions it wraps around.
Route::middleware([MyMiddleware::class])->group(function() {
// routess
});
But if one of the routes has dependency injection (ie, PATCH function in a controller):
public function update(MyModel $model, Request $request) {}
If the MyModel does not exist, then it throws the error that it doesn't exist without running the middleware.
The workaround I have done is this in app.php which forces it to run before the functions:
$middleware->api(prepend: [MyMiddleware::class]);
Is this correct? I feel that this step shouldn't be necessary.
FYI I am using this type of middleware:
return $next($request);