
We have all been there recently. A client calls you up, completely energized. They just saw a flashy new ChatGPT feature online. Suddenly, they demand “AI” sprinkled all over their existing project. Figuring out the best way to handle this used to be a massive headache. You would wrestle with raw HTTP requests and confusing API docs.
Luckily, the openai-php/laravel package changes the game entirely. It is the absolute best way to handle Laravel AI integration right now. In this post, we will look at exactly how to drop it into your codebase. We will build real features you can actually use immediately. We won’t just build a useless, generic chatbot.
Grab a coffee. Open up your code editor. Let’s get your Laravel app talking directly to the future.
Why AI Is No Longer Optional in Laravel Apps (2026)
Let’s be brutally honest about web development right now. Users expect significantly smarter applications today. They absolutely refuse to read a massive wall of text. They want a crisp, accurate summary instantly. They hate waiting two full days for a standard support reply.
If your application feels dumb, your competitors win easily. Adding an AI-powered Laravel app feature is not a gimmick. It is a strict, non-negotiable requirement for modern platforms. Freelance clients feel intense pressure to modernize their businesses. They pass that exact pressure right down to us developers. We must adapt our programming skillsets quickly.
The good news? You do not need a machine learning degree. You just need a solid API client and basic PHP knowledge. We can build incredibly smart features with very little code.
What Is the openai-php/laravel Package?
So, what exactly is this specific tool? It is a super clean, community-maintained wrapper. It sits cleanly around the official OpenAI PHP client. You can view the source code directly at github.com/openai-php/laravel. Brilliant developers in our community actively maintain it.
You could theoretically use Laravel’s native HTTP facade. I tried that painful route initially myself. It gets incredibly messy and complex fast. You have to manage custom headers manually every time. You must format massive JSON payloads perfectly. Catching weird API exceptions gets exhausting quickly.
This specific ChatGPT Laravel package handles all that tedious boilerplate. It gives you a beautiful, fluent facade. It feels completely native to the modern Laravel ecosystem. It remains the undisputed winner for API communication.
Installing and Setting Up openai-php/laravel in Your Laravel Project
Let’s get our hands dirty right now. Pulling this into your active project takes two minutes. First, fire up your local terminal window. Run the standard composer command below.
composer require openai-php/laravel
Once that download finishes, you must publish the configuration. This exposes the config file to your application directory. You can tweak the underlying client settings there later.
php artisan vendor:publish --provider="OpenAI\Laravel\ServiceProvider"
This creates a config/openai.php file in your project. You don’t need to touch it much — all the important stuff goes in your .env file, which is exactly how Laravel developers like it.
Next, open up your project’s .env file. You need to securely add your OPENAI_API_KEY. You grab a fresh key directly from platform.openai.com.
OPENAI_API_KEY=your-api-key-here
OPENAI_ORGANIZATION=your-org-id-here
That is literally the entire setup process. Laravel’s incredible auto-discovery handles the rest automatically. You are ready to start making requests immediately.
To get your API key, head to platform.openai.com, sign in, go to API Keys in the left sidebar, and click Create new secret key. Give it a name like “Laravel App” so you remember what it’s for. Copy it immediately — OpenAI only shows it once.
Paste it into your .env, run php artisan config:clear, and you’re fully set up. No service provider registration, no manual binding — Laravel auto-discovers the package. That’s it. Genuinely.
Making Your First API Call to ChatGPT
Now that we have everything installed, let’s test it. We will write a simple controller method today. We want to ensure everything connects properly to the server. We use the standard create method here.
Let’s write some actual code. Generate a controller:
php artisan make:controller AiController
Open it up and add a simple test method:
<?php
namespace App\Http\Controllers;
use OpenAI\Laravel\Facades\OpenAI;
use Illuminate\Http\Request;
class AiController extends Controller
{
public function test()
{
// Send a chat message to the OpenAI API
$result = OpenAI::chat()->create([
'model' => 'gpt-4o', // Which AI model to use
'messages' => [
[
'role' => 'user', // 'user' = the person asking
'content' => 'Explain Laravel in one sentence for a beginner.',
],
],
'max_tokens' => 100, // Limit how long the response can be
]);
// Extract the text from the response object
$reply = $result->choices[0]->message->content;
return response()->json(['reply' => $reply]);
}
}
Wire it up in routes/web.php:
use App\Http\Controllers\AiController;
Route::get('/ai-test', [AiController::class, 'test']);
Visit /ai-test in your browser. If everything is configured correctly, you’ll see a JSON response with ChatGPT’s answer. That little moment — seeing the AI response come back in your own Laravel app for the first time — is honestly pretty satisfying.
A quick note on choices[0]: OpenAI returns an array of possible responses. By default, you get one. You can request multiple with the n parameter, but for 99% of use cases, choices[0] is all you need.
Also notice the max_tokens parameter. Always set it. We’ll talk more about this in the cost section, but it’s a good habit to start right now.
See how we can use AI with ChatGPT in laravel project with practical examples on https://codewithjayesh.com/openai-php-laravel-add-ai-features-to-your-laravel-app/
Top comments (0)