PHP code example of kosadchiy / laravel-parallel-db
1. Go to this page and download the library: Download kosadchiy/laravel-parallel-db 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/ */
kosadchiy / laravel-parallel-db example snippets
return [
'default_max_concurrency' => 3,
'default_timeout_ms' => 500,
'error_mode' => 'fail_fast', // fail_fast|collect
'pool' => [
'enabled' => true,
'max_idle_per_key' => 3,
],
'drivers' => [
'pgsql' => ['enabled' => true],
'mysql' => ['enabled' => true],
],
];
$result = DB::parallel()->run([
'users' => DB::table('users')->where('active', true),
'servers' => DB::table('servers')->where('status', 'ok'),
]);
$result = DB::parallel()->run([
'users' => User::query()->where('active', true),
'transactions' => DB::table('transactions')->latest()->limit(10),
]);
$users = $result['users']->toEloquentCollection(User::class);
$transactions = $result['transactions']->toCollection();
$status = 'paid';
$from = now()->subDays(30);
$limit = 10;
$result = DB::parallel()->run([
'top_users' => fn () => User::query()
->select('id', 'name')
->withCount('transactions')
->whereHas('transactions', fn ($query) => $query->where('status', $status))
->orderByDesc('transactions_count')
->limit($limit),
'recent_revenue' => function () use ($status, $from) {
return DB::table('transactions')
->selectRaw('date(created_at) as day, sum(amount) as total')
->where('status', $status)
->where('created_at', '>=', $from)
->groupByRaw('date(created_at)')
->orderBy('day');
},
]);
use Kosadchiy\LaravelParallelDb\Enum\ErrorMode;
$result = DB::parallel(
maxConcurrency: 3,
timeoutMs: 1000,
errorMode: ErrorMode::COLLECT,
)->run([
'users' => User::query()->where('active', true),
'servers' => Server::query()->where('status', 'ok'),
]);
$result = DB::connection('pgsql')->parallel()->run([
'users' => User::query()->where('active', true),
]);
bash
php artisan vendor:publish --tag=parallel-db-config