PHPackages                             coderubix/laravel-ai-error-handler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. coderubix/laravel-ai-error-handler

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

coderubix/laravel-ai-error-handler
==================================

AI-powered error handler for Laravel using Perplexity API.

117Blade

Since Aug 24Pushed 8mo agoCompare

[ Source](https://github.com/developer-ashok/laravel-ai-error-handler)[ Packagist](https://packagist.org/packages/coderubix/laravel-ai-error-handler)[ RSS](/packages/coderubix-laravel-ai-error-handler/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel AI Error Handler
========================

[](#laravel-ai-error-handler)

A Laravel package that integrates with Perplexity AI to provide intelligent error analysis and debugging suggestions. When exceptions occur in your Laravel application, this package captures them and uses AI to generate actionable debugging insights.

Features
--------

[](#features)

- **🤖 Dual AI Analysis**: Two-step AI processing for comprehensive error understanding
    - **Detailed Error Explanation**: AI analyzes and explains what went wrong and why
    - **Clean Code Fixes**: AI generates executable PHP code to fix the issue
- **🔧 Automatic Code Fixing**: Apply AI-suggested fixes directly to your files
- **💾 Smart Backup System**: Automatic timestamped backups before applying any fixes
- **🔄 One-Click Restore**: Easily restore files from backups if fixes don't work
- **📋 Copy to Clipboard**: Copy generated code fixes with one click
- **🎨 Beautiful Success Pages**: Professional interface showing fix results and backup details
- **🔍 Comprehensive Logging**: Debug-friendly logging for troubleshooting
- **⚡ Multiple Data Passing Methods**: Robust session handling with URL parameter fallbacks
- **🛡️ Safe Code Extraction**: Filters out explanatory text, only applies executable PHP code
- **📱 Responsive Design**: Clean, modern UI that works on all devices

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1
- Laravel 8.x or higher
- Perplexity AI API key
- Guzzle HTTP client

Installation
------------

[](#installation)

### 1. Install via Composer

[](#1-install-via-composer)

```
composer require coderubix/laravel-ai-error-handler:dev-main
```

**Note**: Since this package is in development, use `:dev-main` to allow development versions.

#### Alternative: Local Development Installation

[](#alternative-local-development-installation)

If you're developing this package locally, you can also add it as a path repository in your Laravel project's `composer.json`:

```
{
    "repositories": [
        {
            "type": "path",
            "url": "/path/to/laravel-ai-error-handler"
        }
    ]
}
```

Then install without version constraint:

```
composer require coderubix/laravel-ai-error-handler
```

### 2. Publish Configuration

[](#2-publish-configuration)

```
php artisan vendor:publish --tag=ai-error-handler-config
```

Optionally, you can also publish the views for customization:

```
php artisan vendor:publish --tag=ai-error-handler-views
```

### 3. Environment Configuration

[](#3-environment-configuration)

Add your Perplexity API key to your `.env` file:

```
PERPLEXITY_API_KEY=your_api_key_here
AI_ERROR_HANDLER_ENABLED=true
AI_ERROR_HANDLER_MODEL=llama-3.1-sonar-large-128k-online
```

Configuration
-------------

[](#configuration)

The package configuration file (`config/ai-error-handler.php`) contains:

```
return [
    'enabled' => env('AI_ERROR_HANDLER_ENABLED', false),
    'perplexity_api_key' => env('PERPLEXITY_API_KEY', ''),
    'model' => env('AI_ERROR_HANDLER_MODEL', 'sonar'),
];
```

### Configuration Options

[](#configuration-options)

- **enabled**: Enable/disable the AI error handler (default: false)
- **perplexity\_api\_key**: Your Perplexity AI API key
- **model**: AI model to use for error analysis (default: 'sonar')

### Available Models

[](#available-models)

You can configure any Perplexity AI model by setting the `AI_ERROR_HANDLER_MODEL` environment variable. The default is `llama-3.1-sonar-large-128k-online`.

**Popular Models:**

- `llama-3.1-sonar-large-128k-online` (default) - Balanced performance and accuracy
- `llama-3.1-sonar-small-128k-online` - Faster responses, lower cost
- `llama-3.1-sonar-huge-128k-online` - Maximum accuracy and detail

Simply set the full model name in your `.env` file.

Usage
-----

[](#usage)

### 🚀 Automatic Error Handling

[](#-automatic-error-handling)

Once enabled, the package automatically intercepts all Laravel exceptions and displays an AI-powered error analysis page with:

1. **Error Details**: Shows the error message, file, and line number
2. **AI Analysis Button**: Click "Get AI Fix" to analyze the error
3. **Dual AI Processing**:
    - **Step 1**: Detailed explanation of what went wrong and why
    - **Step 2**: Clean, executable PHP code to fix the issue
4. **Apply Fixes**: Apply suggested fixes directly with automatic backup
5. **Success Page**: Beautiful results page with file details and restore options

### 🔧 AI Fix Workflow

[](#-ai-fix-workflow)

1. **Error Occurs** → Laravel shows AI error handler page
2. **Click "Get AI Fix"** → AI analyzes the error (dual API calls)
3. **Review Suggestions** → See detailed explanation + code fixes
4. **Apply Fix** → Click "Apply This Fix" (creates automatic backup)
5. **Success Page** → See results, file details, backup info, restore option

### 📋 Copy &amp; Manual Application

[](#-copy--manual-application)

If you prefer not to auto-apply fixes:

1. **Copy Code**: Use the "Copy Code" button for any suggested fix
2. **Manual Review**: Review the code before applying
3. **Manual Application**: Apply the changes yourself in your IDE

### 🔄 Backup &amp; Restore System

[](#-backup--restore-system)

- **Automatic Backups**: Created before every fix with timestamp
- **Backup Location**: `storage/app/ai-error-handler/backups/`
- **One-Click Restore**: Restore from backup if fix doesn't work
- **Backup Management**: Backups are automatically cleaned up after restore

### 📍 Available Routes

[](#-available-routes)

```
POST /ai-error-handler/fix        # AI analysis and fix generation
POST /ai-error-handler/apply-fix  # Apply a specific fix
POST /ai-error-handler/restore    # Restore from backup
GET  /ai-error-handler/success    # Success page after fix/restore

```

How It Works
------------

[](#how-it-works)

### 🔄 Complete Error Resolution Flow

[](#-complete-error-resolution-flow)

1. **Exception Capture**: `AIExceptionHandler` intercepts Laravel exceptions
2. **Error Display**: Shows user-friendly error page with error details
3. **AI Analysis Request**: User clicks "Get AI Fix" button
4. **Dual AI Processing**:
    - **First API Call**: Detailed error explanation and analysis
    - **Second API Call**: Clean, executable PHP code fixes (no explanatory text)
5. **Smart Code Extraction**: Filters and extracts only valid PHP code
6. **Results Display**: Shows both explanation and actionable code fixes
7. **Fix Application**: User can apply fixes with automatic backup creation
8. **Success Feedback**: Professional success page with file details and restore options

### 🤖 AI Processing Details

[](#-ai-processing-details)

- **Two-Step Analysis**: Separate API calls for explanation vs. code generation
- **Safe Code Generation**: AI instructed to generate only executable PHP (no `
