1 week ago

Laravel - Collection of Routes with modification from Str::of

Posted 1 week ago by Mbrown1431

I'm digging my way through the Helpers in the Laravel docs. I thought I'd tool around with getting all routes and modifying some of them with helpers and then display them on a table. It is functioning, however I'd like to modify the $routeMiddleware to only contain characters after the '@' symbol. Currently it displays something like:

Facade\Ignition\Http\Controllers\[email protected]__invoke

Since it is an array, I can't seem to find the proper technique or helper to complete the task as I get an error for array to string conversion. General guidance here for the proper method to do this would be appreciated.


use Illuminate\Support\Str;

class Api
     * Return all the registered routes.
     * @return \Illuminate\Http\JsonResponse
    public function getRoutes()
        $routes = collect(Route::getRoutes())->map(function ($route, $index) {
            $routeName = $route->action['as'] ?? '';
            if (Str::endsWith($routeName, '.')) {
                $routeName = '';

            $routeMiddleware = $route->action['middleware'] ?? [];
            $routeMiddleware = (Str::of($routeMiddleware)->after('@'));
            if (! is_array($routeMiddleware)) {
                $routeMiddleware = [$routeMiddleware];

            return [
                'uri' => $route->uri,
                'as' => $routeName,
                'methods' => $route->methods,
                'action' => $route->action['uses'] ?? '',
                'middleware' => $routeMiddleware,

        return response()->json($routes);

Please sign in or create an account to participate in this conversation.