nosun
1263
2
Laravel

multiple connection use orm and setConnection way,it can not work!

Posted 3 years ago by nosun

I have a project for many site management( the code nearly same), so I need many database connection on the same model.

I use laravel 5.2, and config database setting,then I use setConnection function, after that I use orm function like $model->all(),but it still use default connection.

I use DB::connection() too, it works fine.

the code like this

class Test extends Controller
{
public function index(){

    $a = new Term();
    $a->setConnection('shirley');
    $b = $a->all();
    dd($b);

    $r =DB::connection('shirley')->select('select * from terms limit 10');
    dd($r);
}
}

I check Model code, find it use "new static" before query it ,it will create a new object without the property I just set。

is this a bug, in the model, it give us a function setConnection, but it not works.

How can I resolve it?

Many thanks!

Please sign in or create an account to participate in this conversation.