I'm working with transactions in laravel 5, so far I have been getting the results of any method outside of the statement with a reference param &$, but I red this is a bad practice because the operator "&" is obsolete. Is there any other way to get the value of a var outside transaction scope?
this is a code example:
public function post(Request $request, Persona $persona)
{
try {
DB::transaction(function () use ($request, &$result) {
$result = Persona::create($request->all());
// ... Moooore code omitted
});
// Do more thing with $result
$result;
} // ...
}}
Use DB::beginTransaction()
and DB::commit()
instead:
DB::beginTransaction();
try {
$result = Persona::create($request->all());
DB::commit();
} catch (Exception $e) {
DB::rollBack();
}
It's not that complicated, just return the result inside your transaction ;)
public function post(Request $request, Persona $persona)
{
try {
$result = DB::transaction(function () use ($request) {
$result = Persona::create($request->all());
// ... Moooore code omitted
return $result;
});
// Do more thing with $result
$result;
} // ...
}}