logo
podcast Podcast
get help Get Unstuck

Export all routes to a spreadsheet

Businesses love spreadsheets

Joel Clermont
Joel Clermont
2024-10-29

"Hey there buddy, we need you to export all the routes to an Excel spreadsheet so that Bob can work his magic and generate a project status report."

This might seem like an odd request, but businesses really do love spreadsheets, so let's see what we can do to make this happen.

If we just run artisan route:list, we get very nice output in our console, but it would take a lot of copy/paste massaging to get this into a tabular format.

A better approach is to run artisan route:list --json which generates a structured JSON object containing all the route data.

Then we can use a command line tool called jq to convert this JSON to CSV:

php artisan route:list --json | \
  jq -r '
    (.[0] | keys_unsorted | @csv),
    (.[] | [.domain, .method, .uri, .name, .action, (.middleware | join(";"))] | @csv)
  ' \
  > routes.csv

We take the JSON output from the artisan command and pipe it through jq so that each object in the routes array is converted into a CSV row, and we preserve the header names as the first row. And we're converting the array of middleware to a semicolon-separated string.

Now Bob has his routes.csv file, which will open just fine in Excel, and you didn't have to copy and paste hundreds of routes manually.

Here to help,

Joel

P.S. This is a real question that came up in the Mastering Laravel community this week. Lots of great nuggets like this inside.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.