Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new on other than default connection #17384

Closed
reno1979 opened this issue Jan 18, 2017 · 3 comments
Closed

Create new on other than default connection #17384

reno1979 opened this issue Jan 18, 2017 · 3 comments

Comments

@reno1979
Copy link

reno1979 commented Jan 18, 2017

The model create method keeps using the default db.
We are using artisan tinker, and tried the following:

\App\FeatureGroup::on('otherConnection')->getModel()->create(['name'=>'test']);
// a new row is inserted in the default db, but should be in the otherConnection db
// firstOrCreate does the same :( 

/* OUR WORKAROUND */

$featureGroupModel = new \App\FeatureGroup;
$featureGroupModel->setConnection('otherConnection');
$featureGroupModel->save();

$featureGroupModel->id; // the given id is from the new table row in otherConnection db :)

// or 

\App\FeatureGroup::on('otherConnection')->getModel()->firstOrNew(['name'=>'test'])->save();

@GrahamCampbell
Copy link
Member

Please upgrade to the latest php 7.0.x or 7.1.x.

@themsaid
Copy link
Member

themsaid commented Jan 18, 2017

@reno1979 that fix wasn't related to creating model, just querying related models on the parent connection.

I've submitted a proposal to add a connection argument to the create method, not sure if it'll pass though since you can already do it by constructing the instance yourself.

#17392

@reno1979
Copy link
Author

reno1979 commented Jan 18, 2017

@themsaid Thank you for looking in to this. Constructing a new instance is our current work arround. It just seems odd that the create methods always falls back to the default connection settings.

Using the workaround (instance with correct connection settings) causes a lot of additional code...
It would be more intuitive to just use ::

\App\FeatureGroup::on('otherConnection')->getModel()->create(['name'=>'test']);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants