Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mega_Aleksandar's avatar

Laravel + AlpineJS + InertiaJS

Hello everyone,

I need some of your experience in developing:

  • a frontend with Laravel and AlpineJS
  • a backend with Laravel and InertiJS

What I am trying to achieve is to have the frontend work as a plain Laravel+AlpineJS+TailwindCSS website, while the backend of it would be an SPA with Laravel+InertiaJS+TailwindCSS.

I probably need 2 main.js, 2 different style.css, 2 different starting views (one for frontend, one for the inertia).

Any tips or tricks are welcome, if you are willing to share.

Thank you for your time

0 likes
5 replies
LaryAI's avatar
Level 58

To achieve a setup where you have a frontend with Laravel, AlpineJS, and TailwindCSS, and a backend as an SPA with Laravel, InertiaJS, and TailwindCSS, you can follow these steps:

  1. Setup Laravel Project: First, create a new Laravel project if you haven't already:

    laravel new my-project
    cd my-project
    
  2. Install Dependencies: Install InertiaJS, Vue (or React if you prefer), and TailwindCSS:

    composer require inertiajs/inertia-laravel
    npm install @inertiajs/inertia @inertiajs/inertia-vue3 vue@next
    npm install tailwindcss
    
  3. Configure TailwindCSS: Initialize TailwindCSS:

    npx tailwindcss init
    

    Update your tailwind.config.js to include your paths:

    module.exports = {
      purge: [
        './resources/**/*.blade.php',
        './resources/**/*.js',
        './resources/**/*.vue',
      ],
      darkMode: false, // or 'media' or 'class'
      theme: {
        extend: {},
      },
      variants: {
        extend: {},
      },
      plugins: [],
    }
    
  4. Setup Frontend (Laravel + AlpineJS + TailwindCSS): Create a frontend.js file for your frontend logic:

    // resources/js/frontend.js
    import 'alpinejs';
    import '../css/frontend.css';
    

    Create a frontend.css file for your frontend styles:

    /* resources/css/frontend.css */
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    

    Update your webpack.mix.js to compile the frontend assets:

    mix.js('resources/js/frontend.js', 'public/js')
       .postCss('resources/css/frontend.css', 'public/css', [
          require('tailwindcss'),
       ]);
    
  5. Setup Backend (Laravel + InertiaJS + TailwindCSS): Create a backend.js file for your backend logic:

    // resources/js/backend.js
    import { createApp, h } from 'vue';
    import { createInertiaApp } from '@inertiajs/inertia-vue3';
    import '../css/backend.css';
    
    createInertiaApp({
      resolve: name => require(`./Pages/${name}`),
      setup({ el, App, props, plugin }) {
        createApp({ render: () => h(App, props) })
          .use(plugin)
          .mount(el);
      },
    });
    

    Create a backend.css file for your backend styles:

    /* resources/css/backend.css */
    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    

    Update your webpack.mix.js to compile the backend assets:

    mix.js('resources/js/backend.js', 'public/js')
       .postCss('resources/css/backend.css', 'public/css', [
          require('tailwindcss'),
       ]);
    
  6. Create Separate Views: Create separate views for your frontend and backend:

    <!-- resources/views/frontend.blade.php -->
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="{{ mix('css/frontend.css') }}" rel="stylesheet">
        <title>Frontend</title>
    </head>
    <body>
        <div x-data="{ message: 'Hello, Alpine!' }">
            <h1 x-text="message"></h1>
        </div>
        <script src="{{ mix('js/frontend.js') }}"></script>
    </body>
    </html>
    
    <!-- resources/views/backend.blade.php -->
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="{{ mix('css/backend.css') }}" rel="stylesheet">
        <title>Backend</title>
    </head>
    <body>
        @inertia
        <script src="{{ mix('js/backend.js') }}"></script>
    </body>
    </html>
    
  7. Routing: Define routes for your frontend and backend in web.php:

    // routes/web.php
    use Inertia\Inertia;
    
    Route::get('/', function () {
        return view('frontend');
    });
    
    Route::get('/admin', function () {
        return Inertia::render('Dashboard');
    });
    
  8. Create Inertia Pages: Create a sample Inertia page:

    <!-- resources/js/Pages/Dashboard.vue -->
    <template>
      <div>
        <h1>Dashboard</h1>
      </div>
    </template>
    
    <script>
    export default {
      name: 'Dashboard',
    }
    </script>
    
  9. Compile Assets: Finally, compile your assets:

    npm run dev
    

With this setup, you will have a frontend using Laravel, AlpineJS, and TailwindCSS, and a backend SPA using Laravel, InertiaJS, and TailwindCSS. You can further customize and expand this setup based on your project requirements.

gych's avatar

Why not use Inertia for both ends?

Mega_Aleksandar's avatar

@gych Due to SEO and such. I do not want to go down the road of SSR/SSG, cannot deal with it now.

gych's avatar

@Mega_Aleksandar Ah, okay. I don't have any issues with it personally, but I understand if you prefer something simpler for SEO.

Mega_Aleksandar's avatar

@gych I mean, the idea is to have the frontend act as a "normal" server so it can be deployed anywhere on any server provider. And the backend to act as a SPA for loading times etc... I know I can do it, I am just looking for anyone who had any similar experiences to share them here.

Please or to participate in this conversation.