Laravel 5.8 initiate DB::beginTransaction() on a different database per userLaravel database Design and RelationshipRollback in laravel has no effectLaravel 5.8 showing 404 Not Found errorsIs it possible to “lock” a session in Laravel 5.8?Laravel 5.8 dropdown valueComment Table Giving SQLSTATE[42S02] laravel 5.8Laravel 5.8 refresh database without losing data in tables already existLaravel 5.8 get tables belonging to a userLaravel 5.8 Manual user approval?Php artisan migrate in laravel 5.8 with Ubuntu 18

"This used to be my phone number"

Which GPUs to get for Mathematical Optimization (if any)?

What could make large expeditions ineffective for exploring territory full of dangers and valuable resources?

Are there any satellites in geosynchronous but not geostationary orbits?

"Je suis petite, moi?", purpose of the "moi"?

How much solution to fill Paterson Universal Tank when developing film?

Does unblocking power bar outlets through short extension cords increase fire risk?

Authorship dispute on a paper that came out of a final report of a course?

How do you name this compound using IUPAC system (including steps)?

Did Hitler say this quote about homeschooling?

What is a Romeo Word™?

We get more abuse than anyone else

I have found a mistake on someone's code published online: what is the protocol?

Why teach C using scanf without talking about command line arguments?

In this iconic lunar orbit rendezvous photo of John Houbolt, why do arrows #5 and #6 point the "wrong" way?

Why do jet engines sound louder on the ground than inside the aircraft?

How did Jayne know when to shoot?

What is actually sent/loaded to a microcontroller / stm32

Last-minute canceled work-trip mean I'll lose thousands of dollars on planned vacation

How can I help our ranger feel special about her beast companion?

What makes MOVEQ quicker than a normal MOVE in 68000 assembly?

When will the last unambiguous evidence of mankind disappear?

Is it possible to have a career in SciComp without contributing to arms research?

Making a Dataset that emulates `ls -tlra`?



Laravel 5.8 initiate DB::beginTransaction() on a different database per user


Laravel database Design and RelationshipRollback in laravel has no effectLaravel 5.8 showing 404 Not Found errorsIs it possible to “lock” a session in Laravel 5.8?Laravel 5.8 dropdown valueComment Table Giving SQLSTATE[42S02] laravel 5.8Laravel 5.8 refresh database without losing data in tables already existLaravel 5.8 get tables belonging to a userLaravel 5.8 Manual user approval?Php artisan migrate in laravel 5.8 with Ubuntu 18






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have an app where the user owns their own DB. I am creating a record save which includes a relationship (so two tables updated) and I want to wrap the execution with DB::transaction(). The only issue is the DB name must change user by user.



The $user->db_name contains the data expected and it works well with other selection queries.



If I do this it fails with Call to a member function beginTransaction() on null





DB::disconnect(config('database.default'));
Config::set('database.connections.mysql.database', $user->db_name);

DB::beginTransaction();

try
.
.
$data1->create();
$data2->create();

DB::commit();
catch (Exception $e)
DB::rollback();



If I do this and force a fail on the second create statement, it keeps the record in table from the first create.



DB::beginTransaction();

try

DB::disconnect(config('database.default'));
Config::set('database.connections.mysql.database', $user->db_name);

.
.
$data1->create();
$data2->create();

DB::commit();
catch (Exception $e)
DB::rollback();



How do I achieve the DB change and transaction with rollback on failure against a user's db? Thanks










share|improve this question
























  • Maybe it's a good idea to not create a separate database for each user? There might be a reason why this is not properly working in Laravel.

    – Dees Oomens
    Mar 26 at 13:52

















0















I have an app where the user owns their own DB. I am creating a record save which includes a relationship (so two tables updated) and I want to wrap the execution with DB::transaction(). The only issue is the DB name must change user by user.



The $user->db_name contains the data expected and it works well with other selection queries.



If I do this it fails with Call to a member function beginTransaction() on null





DB::disconnect(config('database.default'));
Config::set('database.connections.mysql.database', $user->db_name);

DB::beginTransaction();

try
.
.
$data1->create();
$data2->create();

DB::commit();
catch (Exception $e)
DB::rollback();



If I do this and force a fail on the second create statement, it keeps the record in table from the first create.



DB::beginTransaction();

try

DB::disconnect(config('database.default'));
Config::set('database.connections.mysql.database', $user->db_name);

.
.
$data1->create();
$data2->create();

DB::commit();
catch (Exception $e)
DB::rollback();



How do I achieve the DB change and transaction with rollback on failure against a user's db? Thanks










share|improve this question
























  • Maybe it's a good idea to not create a separate database for each user? There might be a reason why this is not properly working in Laravel.

    – Dees Oomens
    Mar 26 at 13:52













0












0








0


0






I have an app where the user owns their own DB. I am creating a record save which includes a relationship (so two tables updated) and I want to wrap the execution with DB::transaction(). The only issue is the DB name must change user by user.



The $user->db_name contains the data expected and it works well with other selection queries.



If I do this it fails with Call to a member function beginTransaction() on null





DB::disconnect(config('database.default'));
Config::set('database.connections.mysql.database', $user->db_name);

DB::beginTransaction();

try
.
.
$data1->create();
$data2->create();

DB::commit();
catch (Exception $e)
DB::rollback();



If I do this and force a fail on the second create statement, it keeps the record in table from the first create.



DB::beginTransaction();

try

DB::disconnect(config('database.default'));
Config::set('database.connections.mysql.database', $user->db_name);

.
.
$data1->create();
$data2->create();

DB::commit();
catch (Exception $e)
DB::rollback();



How do I achieve the DB change and transaction with rollback on failure against a user's db? Thanks










share|improve this question
















I have an app where the user owns their own DB. I am creating a record save which includes a relationship (so two tables updated) and I want to wrap the execution with DB::transaction(). The only issue is the DB name must change user by user.



The $user->db_name contains the data expected and it works well with other selection queries.



If I do this it fails with Call to a member function beginTransaction() on null





DB::disconnect(config('database.default'));
Config::set('database.connections.mysql.database', $user->db_name);

DB::beginTransaction();

try
.
.
$data1->create();
$data2->create();

DB::commit();
catch (Exception $e)
DB::rollback();



If I do this and force a fail on the second create statement, it keeps the record in table from the first create.



DB::beginTransaction();

try

DB::disconnect(config('database.default'));
Config::set('database.connections.mysql.database', $user->db_name);

.
.
$data1->create();
$data2->create();

DB::commit();
catch (Exception $e)
DB::rollback();



How do I achieve the DB change and transaction with rollback on failure against a user's db? Thanks







eloquent laravel-5.8






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 16:08









Jonas Staudenmeir

15.3k2 gold badges12 silver badges43 bronze badges




15.3k2 gold badges12 silver badges43 bronze badges










asked Mar 26 at 10:42









TheRealPapaTheRealPapa

1,8955 gold badges33 silver badges88 bronze badges




1,8955 gold badges33 silver badges88 bronze badges












  • Maybe it's a good idea to not create a separate database for each user? There might be a reason why this is not properly working in Laravel.

    – Dees Oomens
    Mar 26 at 13:52

















  • Maybe it's a good idea to not create a separate database for each user? There might be a reason why this is not properly working in Laravel.

    – Dees Oomens
    Mar 26 at 13:52
















Maybe it's a good idea to not create a separate database for each user? There might be a reason why this is not properly working in Laravel.

– Dees Oomens
Mar 26 at 13:52





Maybe it's a good idea to not create a separate database for each user? There might be a reason why this is not properly working in Laravel.

– Dees Oomens
Mar 26 at 13:52












1 Answer
1






active

oldest

votes


















0














Use reconnect():





DB::reconnect(config('database.default'));
Config::set('database.connections.mysql.database', $user->db_name);

DB::beginTransaction();

try
.
.
$data1->create();
$data2->create();

DB::commit();
catch (Exception $e)
DB::rollback();






share|improve this answer























  • Does it work for you?

    – Jonas Staudenmeir
    Apr 4 at 23:26










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55355169%2flaravel-5-8-initiate-dbbegintransaction-on-a-different-database-per-user%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Use reconnect():





DB::reconnect(config('database.default'));
Config::set('database.connections.mysql.database', $user->db_name);

DB::beginTransaction();

try
.
.
$data1->create();
$data2->create();

DB::commit();
catch (Exception $e)
DB::rollback();






share|improve this answer























  • Does it work for you?

    – Jonas Staudenmeir
    Apr 4 at 23:26















0














Use reconnect():





DB::reconnect(config('database.default'));
Config::set('database.connections.mysql.database', $user->db_name);

DB::beginTransaction();

try
.
.
$data1->create();
$data2->create();

DB::commit();
catch (Exception $e)
DB::rollback();






share|improve this answer























  • Does it work for you?

    – Jonas Staudenmeir
    Apr 4 at 23:26













0












0








0







Use reconnect():





DB::reconnect(config('database.default'));
Config::set('database.connections.mysql.database', $user->db_name);

DB::beginTransaction();

try
.
.
$data1->create();
$data2->create();

DB::commit();
catch (Exception $e)
DB::rollback();






share|improve this answer













Use reconnect():





DB::reconnect(config('database.default'));
Config::set('database.connections.mysql.database', $user->db_name);

DB::beginTransaction();

try
.
.
$data1->create();
$data2->create();

DB::commit();
catch (Exception $e)
DB::rollback();







share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 1 at 3:30









Jonas StaudenmeirJonas Staudenmeir

15.3k2 gold badges12 silver badges43 bronze badges




15.3k2 gold badges12 silver badges43 bronze badges












  • Does it work for you?

    – Jonas Staudenmeir
    Apr 4 at 23:26

















  • Does it work for you?

    – Jonas Staudenmeir
    Apr 4 at 23:26
















Does it work for you?

– Jonas Staudenmeir
Apr 4 at 23:26





Does it work for you?

– Jonas Staudenmeir
Apr 4 at 23:26








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55355169%2flaravel-5-8-initiate-dbbegintransaction-on-a-different-database-per-user%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript