Get all routes that start with rt_ The code reads all my routes and return to my AJAX request the names of the routes without duplicates and just those that have the &quot;rt_&quot; prefix.
class RouteController extends Controller {
    public function getRoutes()
    {
        
        //Get all the routes.
        $routes = Route::getRoutes();

        
        //Setting up the vars
        $array = [''];

        
        
        foreach ($routes as $route) {
            $condicional = substr($route-&gt;getName(), 0, 3);
            $found = FALSE;

           //Filtering the routes with rt_ prefix
            if ($condicional == 'rt_') {

                 //Filering duplicates
                foreach ($array as $item) {
                    if ($item == $route-&gt;getName()) {
                        $found = TRUE;


                    }

                }

                
                //Echo without duplicates
                if ($found == FALSE) {
                    $array[] = $route-&gt;getName();

                }
            }
        }

        return response()-&gt;json($array, 200);

    }


}
One way to simplify this would be to create a collection with collect() and then call getName() on each route using Collection::map(). Then use collection::filter() to filter out the routes that don't start with rt_ using the string helper method Str::startsWith()
class RouteController extends Controller {
    public function getRoutes()
    { 
        // call getName on each route and then get unique values
        $uniqueRoutes = collect(Route::getRoutes())
            -&gt;map(fn($route) =&gt; $route-&gt;getName())
            -&gt;unique(); 
        //filter out any routes not starting with rt_
        $filteredRoutes = $uniqueRoutes-&gt;filter(function($name) {
             return Str::startsWith($name, 'rt_');
        });
        return response()-&gt;json($filteredRoutes, 200); 
    }
}

Note - the uses the PHP 7.4 arrow function syntax could be used to simplify the callback to the filter() method
One could also use collection::reduce() to only add routes to the list that contain the desired prefix.

The existing code contains this block:

          //Filering duplicates
           foreach ($array as $item) {
               if ($item == $route-&gt;getName()) {
                   $found = TRUE;
               }
           }


That could be simplified using the PHP function in_array().

Another suggestion is to always use strict equality comparisons (i.e. === and !==) when possible - e.g. when $found should either be true or false then compare without the possibility of type conversion (i.e. with ==).