1. Go to this page and download the library: Download symbiotic/eloquent 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/ */
symbiotic / eloquent example snippets
$config = [
'default' => 'my_connect_name',
// Connections by package namespaces
'namespaces' => [
'\\Modules\\Articles' => 'mysql_dev',
]
'connections' => [
'my_connect_name' => [
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => 'toor',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
'mysql_dev' => [
// ....
],
]
];
// Building from an array of data {@link https://github.com/symbiotic-php/database}
$connectionsConfig = \Symbiotic\Database\DatabaseManager::fromArray($config);
// Manager initialization
$manager = new \Symbiotic\Database\Eloquent\EloquentManager(
$connectionsConfig,
\Symbiotic\Database\Eloquent\SymbioticModel::class // Base model class
// If you are using Laravel models - \Illuminate\Database\Eloquent\Model::class
);
// Additionally, you can install the Event Manager (\Illuminate\Contracts\Events\Dispatcher)
$manager->setEventDispatcher(new Dispatcher());
// Activating your connection manager for models
$manager->bootEloquent();
// Your code and requests....
// Extracting the current connection manager and installing the previous one (if any)
$manager->popEloquent();
use Symbiotic\Database\Eloquent\SymbioticModel;
class User extends SymbioticModel
{
//....
}
/**
* @var \Symbiotic\Database\Eloquent\EloquentManager $manager
*/
// Returns the Laravel connection manager {@see \Illuminate\Database\DatabaseManager}
$laravelDatabase = $manager->getDatabaseManager();
// Getting a Connection Object {@see \Illuminate\Database\Connection}
// if called without a parameter, it will return the connection 'default'
$connection = $manager->getConnection($connectionName ?? null);
// Adding a connection
$manager->addConnection(
[
'driver' => 'mysql',
'database' => 'test_db',
'username' => 'root',
'password' => 'toor',
//....
],
'test_connection'
);
// Installing an Event Dispatcher for Models {@uses \Illuminate\Contracts\Events\Dispatcher}
$manager->setEventDispatcher($dispatcher);
// Getting the event dispatcher, if the dispatcher is not set, will return the anonymous class NullDispatcher
$dispatcher = $manager->getEventDispatcher();
// Force model connection switching at runtime
$manager->withConnection('connection_name', function() {
$model = new MyModel();
$model->save([]);
});
/**
* @var \Symbiotic\Database\Eloquent\EloquentManager $manager
* @var \Symbiotic\Database\DatabaseManager $symbioticDatabase
*/
// Returns the Symbiotic connection manager {@see \Symbiotic\Database\DatabaseManager}
// Read the documentation of the manager and the features of his behavior {@link https://github.com/symbiotic-php/database}
$symbioticDatabase = $manager->getSymbioticDatabaseManager();
// Is the connection search by namespace active?
$bool = $symbioticDatabase->isActiveNamespaceFinder();
// Enable/disable search by namespaces
$symbioticDatabase->activateNamespaceFinder(false);
// Adding a separate connection for a module
$symbioticDatabase->addNamespaceConnection('\\Modules\\PagesApplication', 'test_connection');
// Getting the name of the connection by class, if disabled, it will return null
$pagesConnectionName = $symbioticDatabase->getNamespaceConnection(\Modules\PagesApplication\Models\Event::class); // return `test_connection`
// Automatic search for a connection along the call stack via debug_backtrace, if disabled, returns null
$connectionData = $symbioticDatabase->findNamespaceConnectionName();
/**
* @var \Symbiotic\Database\Eloquent\EloquentManager $manager
*/
$capsuleOne = new \Symbiotic\Database\Eloquent\EloquentManager($config,\Illuminate\Database\Eloquent\Model::class);
$capsuleTwo = new \Symbiotic\Database\Eloquent\EloquentManager($config,\Symbiotic\Database\Eloquent\SymbioticModel::class);
$capsuleThree = new \Symbiotic\Database\Eloquent\EloquentManager($config,\Illuminate\Database\Eloquent\Model::class);
$capsuleOne->bootEloquent();
// we work with the first manager
$capsuleOne->popEloquent();
$capsuleThree->bootEloquent();
// activate two managers at once
$capsuleOne->bootEloquent();
$capsuleTwo->bootEloquent();
// working with a second manager
$capsuleTwo->popEloquent();
// we work with the first manager
$capsuleOne->popEloquent();
// working with a third manager
$capsuleOne->popEloquent();
// terminated managers, if Laravel is initiated its connection manager will be installed back