79130704

Date: 2024-10-27 13:51:33
Score: 0.5
Natty:
Report link

If you're manually setting up Inertia in your Laravel project (without using a pre-configured boilerplate), you might run into issues with accessing Laravel named routes in JavaScript. Here’s how to make sure your routes work properly:

  1. Add the Ziggy package to your Laravel project. Ziggy enables using Laravel named routes in JavaScript.
composer require tightenco/ziggy
  1. Use the @routes Directive in Your Layout In your main layout file which is usually app.blade.php, include the @routes directive. This will generate JavaScript versions of your Laravel routes. Your layout should look something like this:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
    
    <!-- Include Laravel routes in JavaScript -->
    @routes
    @viteReactRefresh
    @vite('resources/js/app.jsx')
    @inertiaHead
  </head>
  <body>
    @inertia
  </body>
</html>

Common issue If you see that @routes isn’t recognized and instead renders as plain text, try using it with parentheses like this:

@routes()
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @routes
  • User mentioned (0): @routes
  • User mentioned (0): @routes
  • Low reputation (1):
Posted by: ilyas mohetna