broqit/fields-ai
Laravel Nova field that brings AI to your admin forms, letting you generate and refine text content directly in fields. Adds configurable prompts/actions to speed up writing, editing, and content creation inside Nova resources.
Installation
composer require broqit/fields-ai
Publish the config file (if available):
php artisan vendor:publish --provider="Broqit\FieldsAI\FieldsAIServiceProvider"
Basic Usage
Inject the FieldsAI facade into your controller or service:
use Broqit\FieldsAI\Facades\FieldsAI;
// Generate a field description
$description = FieldsAI::describeField('user', 'email');
First Use Case Use in a form builder to auto-generate field labels, placeholders, or validation rules:
$label = FieldsAI::generateLabel('user', 'email'); // "User Email Address"
$placeholder = FieldsAI::generatePlaceholder('user', 'email'); // "example@domain.com"
Dynamic Form Field Generation
// In a FormRequest or FormService
public function rules()
{
$rules = [];
$fields = ['name', 'email', 'bio'];
foreach ($fields as $field) {
$rules[$field] = FieldsAI::inferValidation($field);
}
return $rules;
}
Localization Support
// Generate labels in multiple languages
$labels = [
'en' => FieldsAI::generateLabel('product', 'price', 'en'),
'es' => FieldsAI::generateLabel('product', 'price', 'es'),
];
Integration with Laravel Blade
@foreach($formFields as $field)
<div>
<label>{{ FieldsAI::generateLabel($model, $field) }}</label>
<input type="text" name="{{ $field }}">
<span class="help-text">{{ FieldsAI::generateHelpText($model, $field) }}</span>
</div>
@endforeach
API Response Enhancement
// Auto-generate API response metadata
$response = [
'data' => $user,
'fields' => [
'email' => FieldsAI::describeField('user', 'email'),
'created_at' => FieldsAI::describeField('user', 'created_at'),
],
];
Model Casting
// Auto-generate attribute descriptions for API docs
class User extends Model
{
public function getAttributeDescriptions()
{
return collect($this->attributes)
->mapWithKeys(fn ($value, $key) => [
$key => FieldsAI::describeField('user', $key)
]);
}
}
Rate Limiting
$label = Cache::remember("fields_ai_{$model}_{$field}", now()->addHours(1), function() use ($model, $field) {
return FieldsAI::generateLabel($model, $field);
});
Model/Field Mismatches
model or field names are ambiguous (e.g., user vs. users).FieldsAI::describeField('App\Models\User', 'email'); // Explicit namespace
Contextual Overrides
password field that’s actually a "PIN").// app/Providers/FieldsAIServiceProvider.php
public function boot()
{
FieldsAI::extend('user', 'pin', function ($model, $field) {
return "4-digit security code (e.g., 1234)";
});
}
Configuration Quirks
fields-ai config may not be published by default. Check for:
config('fields-ai.api_key'); // Ensure this is set
config('fields-ai.cache_enabled'); // Toggle caching
Enable Verbose Logging
Add to config/fields-ai.php:
'debug' => env('FIELDS_AI_DEBUG', false),
Then check storage/logs/laravel.log for AI response details.
Mocking for Testing
Use the FieldsAI facade’s mocking methods:
FieldsAI::shouldReceive('generateLabel')
->once()
->with('user', 'email')
->andReturn('Test Email');
$this->assertEquals('Test Email', FieldsAI::generateLabel('user', 'email'));
Fallback Logic Provide defaults for when the AI fails:
$label = FieldsAI::generateLabel('user', 'email', [
'fallback' => ucfirst(str_replace('_', ' ', $field)),
]);
Custom Prompts Override the default AI prompts via config:
'prompts' => [
'label' => 'Generate a user-friendly label for the {field} field in a {model} resource.',
'validation' => 'Suggest Laravel validation rules for the {field} field in a {model}.',
],
Field Type Hints Pass additional context to improve accuracy:
FieldsAI::describeField('order', 'status', [
'type' => 'enum',
'options' => ['pending', 'shipped', 'delivered'],
]);
Batch Processing
Use the batch() method for efficiency:
$descriptions = FieldsAI::batch(['user' => ['email', 'password']]);
// Returns: ['user.email' => "...", 'user.password' => "..."]
How can I help you explore Laravel packages today?