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

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?