“Transacciones de Laravel” Código de respuesta

Transacciones de Laravel

DB::beginTransaction();

try {
    DB::insert(...);
    DB::insert(...);
    DB::insert(...);

    DB::commit();
    // all good
} catch (\Exception $e) {
    DB::rollback();
    // something went wrong
}
404 Not Found

Transacción modelo Laravel

DB::beginTransaction();
        try {
            $project = Project::find($id);
            $project->users()->detach();
            $project->delete();
            DB::commit();
        } catch (\Exception $ex) {
            DB::rollback();
            return response()->json(['error' => $ex->getMessage()], 500);
        }
Shadow

Transacton Laravel Query Builder

DB::beginTransaction();

try {
    DB::insert(...);    
    DB::commit();
} catch (\Throwable $e) {
    DB::rollback();
    throw $e;
}
Brainy Booby

transacción laravel

DB::beginTransaction();
try { /** Statement */   DB::commit(); } 
catch (\Exception $e) { /** Statement if failed */ DB::rollback(); }
mr.dar

transacción de Laravel

// Start transaction!
DB::beginTransaction();

try {
    // Validate, then create if valid
    $newAcct = Account::create( ['accountname' => Input::get('accountname')] );
} catch(ValidationException $e)
{
    // Rollback and then redirect
    // back to form with errors
    DB::rollback();
    return Redirect::to('/form')
        ->withErrors( $e->getErrors() )
        ->withInput();
} catch(\Exception $e)
{
    DB::rollback();
    throw $e;
}

try {
    // Validate, then create if valid
    $newUser = User::create([
        'username' => Input::get('username'),
        'account_id' => $newAcct->id
    ]);
} catch(ValidationException $e)
{
    // Rollback and then redirect
    // back to form with errors
    DB::rollback();
    return Redirect::to('/form')
        ->withErrors( $e->getErrors() )
        ->withInput();
} catch(\Exception $e)
{
    DB::rollback();
    throw $e;
}

// If we reach here, then
// data is valid and working.
// Commit the queries!
DB::commit();

transacción de Laravel

DB::transaction(function()
{
    $newAcct = Account::create([
        'accountname' => Input::get('accountname')
    ]);

    $newUser = User::create([
        'username' => Input::get('username'),
        'account_id' => $newAcct->id,
    ]);
});

Respuestas similares a “Transacciones de Laravel”

Preguntas similares a “Transacciones de Laravel”

Más respuestas relacionadas con “Transacciones de Laravel” en PHP

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código