How do I get Lumen to use DB::transaction?

3.8k views Asked by At

I'm using Lumen with a SQLite DB. I've tried everything I can think of to use DB::transaction in Lumen, but to no avail. I consistently get errors like this:

ReflectionException: /vendor/illuminate/container/Container.php line 779

I've tried putting use DB; at the top of the class. I've tried useing the facade. Nothing seems to work.

Simple example of trying to use it:

DB::transaction(function () use ($attributes, $service) {
   $this->person = $this->person->create([]);
   // do some other stuff
});
1

There are 1 answers

0
jeteon On BEST ANSWER

I've struggled to use transactions using DB::transaction in Lumen myself. A workaround that I ended up using is to just resolve the database using the global function app() instead of the facade and then use the transaction on that like so:

app('db')->transaction(function() {
    // DB work
});

If there are a lot of these you can also cache the result of calling app('db') in a variable for reuse. It will behave just like the static calls on DB.