Installation
Requirements
- PHP 8.2 or higher. Laravel 13 itself needs PHP 8.3.
- Laravel 11, 12 or 13.
- An OpenAI API key. OpenAI bills every call to your OpenAI account.
The package has no migrations, routes or views, so there is nothing to migrate or to link.
Step 1: install the package
composer require darvis/laravel-ai-generator
Laravel discovers the service provider by itself. You don’t register anything.
Step 2: set the API key
Create a key in the OpenAI dashboard, under API keys, and put it in the .env file of your application:
OPENAI_API_KEY=your-api-key-here
Keep the key out of version control: .env is not committed, and .env.example gets an empty value.
That is the only setting you need. Every other setting has a default; see Configuration.
Step 3 (optional): publish the config file
You only need this when you want to change a setting in PHP instead of in .env.
php artisan vendor:publish --tag=ai-generator-config
This copies the file to config/ai-generator.php in your application.
Check that it works
Do this in two steps. The first one does not call OpenAI, so it costs nothing.
1. The package is loaded and the key is read
Run this in the root of your application. Tinker is the command line that ships with a new Laravel application.
php artisan tinker --execute="echo get_class(app(\Darvis\LaravelAiGenerator\Contracts\AiContentDriver::class)), PHP_EOL, \Darvis\LaravelAiGenerator\Support\AiGeneratorConfig::openAiApiKey() === null ? 'key missing' : 'key found', PHP_EOL;"
You should see exactly this:
Darvis\LaravelAiGenerator\Drivers\OpenAiDriver
key found
key missing: the application does not seeOPENAI_API_KEY. See Troubleshooting.Unsupported AI driver: ...:AI_GENERATOR_DRIVERholds a name the package does not know. See Troubleshooting.- An error that says the interface is not instantiable or a class is not found: the service provider is not loaded. Run
composer installand thenphp artisan package:discover.
2. One real call, without an image
This sends one text request to OpenAI and is billed to your account. includeImage: false keeps it to one call and maxWords: 100 keeps the text short.
php artisan tinker --execute="echo app(\Darvis\LaravelAiGenerator\AiGenerator::class)->generate(new \Darvis\LaravelAiGenerator\ContentRequest(topic: 'Why automated tests matter', language: 'en', maxWords: 100, includeImage: false))->title, PHP_EOL;"
After some seconds you see one line: the title the model wrote. The words differ on every run.
When you see an exception instead, its message tells you what went wrong. Every message is listed in Troubleshooting.
Laravel Boost
The package ships a Laravel Boost guideline and a skill in resources/boost/. Run php artisan boost:install, or php artisan boost:update --discover in a project that already uses Boost. Your AI assistant then knows the API, the defaults and the pitfalls: the image that is generated by default, the custom driver binding and how to test without calling OpenAI.
Next steps
- Usage: a complete example and every request option.
- Configuration: change the model, the default language or the timeout.