DeveloperJoy Logo DeveloperJoy

Building an AI SaaS with PHP: Leveraging Laravel, Inertia, and React

June 16, 2024
Building an AI SaaS with PHP: Leveraging Laravel, Inertia, and React

Building an AI-driven Software as a Service (SaaS) can be a rewarding yet challenging endeavor.

Utilizing PHP with robust frameworks like Laravel, Inertia, and React can greatly enhance both development velocity and user experience.

This article walks you through the essential steps to create an AI SaaS using these technologies.

Step 1: Setting Up Laravel

Start by installing Laravel. If you don't have Composer installed, download and install it from

composer create-project --prefer-dist laravel/laravel your-ai-saas

Then you need to install Jetstream with Inertia, Server Side Rendering (SSR), and team management.

composer require laravel/jetstream
php artisan jetstream:install inertia --ssr --teams

Last step, navigate to the project directory:

cd your-ai-saas

Step 2: Interaction between Laravel and React

You already have Inertia.js, the Inertia Laravel adapter, and the React adapter in your project.

Now you can interact with React components from your Laravel backend like this.

Create a React component, for example, Dashboard.js inside resources/js/Pages:

// resources/js/Pages/Dashboard.js
import React from 'react';

const Dashboard = () => {
  return (
      <h1>Welcome to Your AI SaaS</h1>

export default Dashboard;

In your routes routes/web.php, return an Inertia view:

use Inertia\Inertia;

Route::get('/dashboard', function () {
    return Inertia::render('Dashboard');

Step 3: Using OpenAI PHP Client

To cover the AI part, we are going to use the OpenAI PHP client library, you can install it via Composer:

composer require openai-php/client

Set up the client in your Laravel project, usually in a Service Provider:

// AppServiceProvider.php
use OpenAI\Client;

public function register()
    $this->app->singleton(Client::class, function ($app) {
        return \OpenAI::client(env('OPENAI_API_KEY'));

Step 5: Creating an AI Feature

To integrate the AI feature, create a Controller to handle requests:

// app/Http/Controllers/AiController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use OpenAI\Client;

class AiController extends Controller
    public function generate(Request $request, Client $client)
        $response = $client->completions()->create([
            'engine' => 'gtp4o',
            'prompt' => $request->input('prompt'),
            'max_tokens' => 150,

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

Add a route for this controller in routes/api.php:

use App\Http\Controllers\AiController;

Route::post('/generate', [AiController::class, 'generate']);

Create a React component for the AI feature:

// resources/js/Pages/Generate.js
import React, { useState } from 'react';
import axios from 'axios';

const Generate = () => {
  const [prompt, setPrompt] = useState('');
  const [response, setResponse] = useState('');

  const handleSubmit = async () => {
    const result = await'/api/generate', { prompt });

  return (
      <textarea value={prompt} onChange={(e) => setPrompt(} />
      <button onClick={handleSubmit}>Generate</button>

export default Generate;

By combining Laravel, Inertia, and React, and utilizing the OpenAI PHP client, you can create a responsive and intelligent AI SaaS.

The Laravel framework provides a strong backend infrastructure, while Inertia.js ensures smooth client-server interactions, and React offers dynamic user interfaces.

This synergistic stack not only accelerates development but also enhances the overall user experience.

← Go back to the blog

Work with me

Do you own a company or need help with your Laravel project? I can help you with that. Check the plans and let me know if you have any questions.

Get 1 month free with yearly plan during checkout.

Technical Advisor

$ 3 ,995


What's included:

  • Lead your team to a better code architecture, testing mentality, clean code, and more.
  • Lead knowledge-sharing sessions depending on current company status and requirements.
  • Help with product and technical decisions.
  • Pause or cancel anytime.

Integrated Senior Developer

$ 5 ,995


Not available

What's included:

  • Includes all Technical Advisor services.
  • Engages as a team member in daily tasks.
  • Participates actively in day-to-day development.
  • Direct communication:
    Slack, and meetings included.
  • Pause or cancel anytime.

Want to talk first?

Ok, just book a call now.