orchestra/database
Orchestra Database extends Laravel’s database layer for Orchestra Platform, providing helpers and integration for connections, schema, migrations, and query features. Useful for building and maintaining app data structures consistently across Orchestra-based projects.
Installation:
composer require orchestra/database
Register the package in config/app.php under providers:
Orchestra\Database\DatabaseServiceProvider::class,
Orchestra\Database\SearchServiceProvider::class,
Orchestra\Database\ConsoleServiceProvider::class,
Orchestra\Database\CachableQueryServiceProvider::class,
First Use Case:
Enable caching for Eloquent queries via the remember() and rememberForever() macros:
use Orchestra\Database\Eloquent\Builder;
$users = Builder::where('active', 1)->remember(60)->get();
Orchestra\Database\DatabaseServiceProvider for database extensions.Orchestra\Database\CacheDecorator for caching query results.orchestra:migrate or orchestra:migration:refresh for advanced migration workflows.Caching Queries:
// Cache results for 10 minutes
$activeUsers = User::where('status', 'active')->remember(600)->get();
// Cache indefinitely
$admins = User::role('admin')->rememberForever()->get();
Cache Decorator:
// Cache plucked results
$userNames = User::pluck('name')->remember(3600);
// Cache lists (e.g., for dropdowns)
$userEmails = User::lists('email', 'id')->remember(3600);
Migration Commands:
# Run migrations from a specific path
php artisan orchestra:migrate --path=database/migrations/custom
# Refresh migrations with realpath support
php artisan orchestra:migration:refresh --realpath
Search Service Provider:
Integrate with Laravel Scout or custom search logic via Orchestra\Database\SearchServiceProvider.
Extend Eloquent:
Use CachableQueryServiceProvider to add remember() to all Eloquent queries globally.
// config/orchestra/database.php
'cachable' => [
'enabled' => true,
'default_ttl' => 60, // Default TTL in seconds
],
Custom Cache Keys: Override cache keys for specific queries:
$users = User::where('active', 1)->remember(60, 'active_users_cache_key')->get();
Fallback Logic:
Leverage the CacheDecorator fallback to database when cache fails:
$data = Cache::remember('key', 60, function () {
return User::where('active', 1)->get();
});
Cache Invalidation:
Cache::forget('active_users_cache_key');
Performance Overhead:
rememberForever() sparingly; it bypasses cache invalidation entirely.Service Provider Conflicts:
DatabaseServiceProvider, CachableQueryServiceProvider, etc.) are registered.config/app.php.Deprecated Methods:
MigrationServiceProvider and CacheDecorator::lists() are deprecated. Use lists() from CacheDecorator or Eloquent’s pluck().Cache Issues:
Cache::getStore()->get('key'); // Inspect raw cache
Query Caching:
$users = User::where('active', 1)->remember(0)->get(); // Disables caching
Migration Errors:
--verbose for detailed output:
php artisan orchestra:migrate --verbose
database/migrations/.Custom Cache Drivers:
Extend Orchestra\Database\CacheDecorator to support non-standard cache backends:
Cache::extend('custom', function () {
return new CustomCacheStore();
});
Macro Overrides:
Override remember() behavior globally:
Builder::macro('remember', function ($seconds, $key = null) {
// Custom logic here
return $this->macro('cache', $seconds, $key);
});
Search Integration:
Extend Orchestra\Database\SearchServiceProvider for custom search logic:
Search::extend('custom', function () {
return new CustomSearchEngine();
});
Prefix Global Functions:
The package prefixes global functions (e.g., \Cache::remember()) to avoid lookup overhead. Ensure your IDE is configured to recognize these.
Static Methods:
Prefer static methods (e.g., CacheDecorator::pluck()) over instance methods for micro-optimizations.
Batch Caching: Cache collections of related data in bulk:
$users = User::with('posts')->remember(3600)->get();
How can I help you explore Laravel packages today?