Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there another way to "setConnection" on an Eloquent Model?

I am currently handling a "multi db on the fly swap connections" sort of project.

So what I end up doing is the following:

$connectionName = uniqid();
\Config::set('database.connections.' . $connectionName, [/** db options **/]);
\Artisan::call('migrate', ['--database' => $connectionName]);

or

$connectionName = uniqid();           
\Config::set('database.connections.' . $connectionName,[/** db options **/]);

$user = new User();
$user->setConnection($connectionName);
$user->first_name = 'Daisy';
$user->last_name = 'Demo';
$user->is_not_being_ignored_by_santa_this_year = 0;
$user->email = //and so so on
$user->save();

For the Artisan call I sort of understand why Laravel needs to refer to a connection in a string, saved in a config array.

However on the Eloquent Model itself I find it somehow cumbersome to have to write my DB connection into a config array. So it can be picked up by the "Singleton approach" \Config::get().. in the Model.

Is there something more elegant, where I can inject a configuration directly without having to write it into some super global ?

Or am I missing something ?

like image 757
chickenchilli Avatar asked Dec 21 '16 22:12

chickenchilli


People also ask

Is eloquent an ORM?

Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application's data.

Can we use two database in Laravel?

By default, Laravel already provides several database connections such as sqlite, mysql, pgsql and sqlsrv. Because in this article we will try to create multiple database connections in laravel using mysql, so we need to add a mysql connection like the code above.

How do I search in Laravel eloquent?

Searching Eloquent models Imagine you need to provide a search for users. Using Eloquent you can perform a search like this: User::query() ->where('name', 'LIKE', "%{$searchTerm}%") ->orWhere('email', 'LIKE', "%{$searchTerm}%") ->get();


2 Answers

You would probably be better off creating a configuration array for each connection then you could switch between connections pretty easily by specifying which connection to use.

If you need to use multiple connections on the same model you can use the on method:

so it would be something like User::on('dbconnection2')->find(1)

If you just want to use different connections for different models, you can set the protected $connection property on the model:

class User extends Model
{
    protected $connection = 'dbconnection2';
}

Hope that helps.

like image 121
Bryan Avatar answered Sep 28 '22 07:09

Bryan


You could create a factory for your models and pass it the connection on bootstrapping your app:

<?php

class ModelFactory
{
    private $db;

    public function __construct($dbConnection)
    {
        $this->db = $dbConnection;
    }

    public function createNewModel($class)
    {
        $object = new $class();
        $object->setConnection($this->db);
        return $object;
    }
}

Then in your code:

$user = $factory->createModel(User::class);

Something like this! Good luck! :-)

like image 34
delboy1978uk Avatar answered Sep 28 '22 08:09

delboy1978uk