framework icon indicating copy to clipboard operation
framework copied to clipboard

How to change model connection on runtime?

Open Premke opened this issue 13 years ago • 3 comments

How to change model connection on runtime without extending eloquent method/s?

There is a method on which returns Eloquent\Builder, but then this won't work:

User::on('master')->create($data);

while this will:

User::on('master')->insert($data);

However this way model events can't be used.

Premke avatar Jul 06 '13 16:07 Premke

You have to use User::connection('master')->insert();

Sent from my iPhone

On 06/07/2013, at 12:26, Premke [email protected] wrote:

How to change model connection on runtime without extending eloquent method/s?

There is a method on which returns Eloquent\Builder, but then this won't work:

User::on('master')->create($data); while this will:

User::on('master')->insert($data); However this way model events can't be used.

— Reply to this email directly or view it on GitHub.

ipalaus avatar Jul 06 '13 16:07 ipalaus

The on method returns a Builder object, not a Model object. That's why the create method and model events do not work. There is a setConnection method or you could call getModel on the Builder object.

ronaldcastillo avatar Jul 06 '13 16:07 ronaldcastillo

@ipalaus When using insert model events are not triggered.

@ronaldcastillo Thanks for clearing that up, I've ended up using getModel.

Premke avatar Jul 06 '13 20:07 Premke