PHP code example of mostafaarafat / laravel-datachat
1. Go to this page and download the library: Download mostafaarafat/laravel-datachat library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mostafaarafat / laravel-datachat example snippets
return [
// AI Provider
'ai_provider' => env('DATACHAT_AI_PROVIDER', 'anthropic'),
// Database connection
'connection' => env('DATACHAT_DB_CONNECTION', null),
// Security
'strict_mode' => env('DATACHAT_STRICT_MODE', true),
// Queue settings
'queue' => [
'enabled' => env('DATACHAT_QUEUE_ENABLED', true),
'connection' => env('DATACHAT_QUEUE_CONNECTION', 'redis'),
'queue_name' => env('DATACHAT_QUEUE_NAME', 'datachat'),
],
// Cache
'cache' => [
'enabled' => env('DATACHAT_CACHE_ENABLED', true),
'ttl' => env('DATACHAT_CACHE_TTL', 3600),
],
// Rate limiting
'rate_limits' => [
'messages_per_day' => env('DATACHAT_MAX_MESSAGES_PER_DAY', 100),
'messages_per_minute' => env('DATACHAT_MAX_MESSAGES_PER_MINUTE', 10),
],
// Widget defaults
'widget_defaults' => [
'name' => 'DataChat',
'primary_color' => '#3b82f6',
'position' => 'bottom-right',
'greeting_message' => 'Hi! Ask me anything about your data.',
],
// CORS
'cors' => [
'allowed_origins' => env('DATACHAT_ALLOWED_ORIGINS', '*'),
'allowed_methods' => ['GET', 'POST', 'OPTIONS'],
'allowed_headers' => ['Content-Type', 'X-DataChat-Key', 'X-Requested-With'],
],
];
protected function applyScopingRules(array $metadata): void
{
if (isset($metadata['user_id'])) {
DB::table('orders')->where('user_id', $metadata['user_id']);
}
}
// In widget configuration
$widget->update([
'allowed_domains' => ['example.com', 'app.example.com']
]);
use Mostafaarafat\DataChat\Models\ChatConfig;
$widget = ChatConfig::create([
'user_id' => auth()->id(),
'widget_name' => 'Sales Assistant',
'primary_color' => '#ff6b6b', // Brand color
'position' => 'bottom-left', // or 'bottom-right'
'greeting_message' => 'Hello! I can help you find sales data.',
'max_messages_per_day' => 500,
]);
use Mostafaarafat\DataChat\Models\Suggestion;
Suggestion::create([
'config_id' => $widget->id,
'question' => 'Show me today\'s revenue',
'category' => 'sales',
'display_order' => 1,
]);
use Mostafaarafat\DataChat\Models\Usage;
$usage = Usage::where('config_id', $widgetId)
->where('date', '>=', now()->subDays(30))
->get();
$totalMessages = $usage->sum('message_count');
$totalCost = $usage->sum('ai_api_cost');
use Mostafaarafat\DataChat\Models\Conversation;
$conversations = Conversation::where('config_id', $widgetId)
->with('messages')
->latest('last_message_at')
->get();
// In widget configuration
$widget->update([
'allowed_domains' => ['your-domain.com']
]);
bash
# Publish config file
php artisan vendor:publish --tag=datachat-config
# Run migrations
php artisan migrate
# Publish widget assets
php artisan vendor:publish --tag=datachat-assets
bash
php artisan queue:work --queue=datachat
bash
php artisan datachat:install
bash
php artisan datachat:generate-key {widget_id}
bash
# Check if files exist
ls -la public/vendor/datachat/
# If missing, republish:
php artisan vendor:publish --tag=datachat-assets --force
bash
php artisan datachat:generate-key {widget_id}
bash
# Restart queue
php artisan queue:restart
# Check failed jobs
php artisan queue:failed
# Retry failed jobs
php artisan queue:retry all
bash
composer install --optimize-autoloader --no-dev
php artisan config:cache
php artisan route:cache
php artisan view:cache
http
GET /api/datachat/conversation/{id}
Headers:
X-DataChat-Key: dc_your_api_key
Response:
{
"messages": [
{
"id": 1,
"role": "user",
"content": "How many orders today?",
"created_at": "2025-03-30T10:30:00Z",
"has_error": false
},
{
"id": 2,
"role": "assistant",
"content": "You have 47 orders today.",
"created_at": "2025-03-30T10:30:03Z",
"has_error": false
}
]
}
http
GET /api/datachat/conversation/{id}/poll?after_id=2
Headers:
X-DataChat-Key: dc_your_api_key
Response:
{
"messages": [
{
"id": 3,
"role": "assistant",
"content": "Latest response...",
"created_at": "2025-03-30T10:31:00Z",
"has_error": false
}
]
}