Krystian
Krystian

Reputation: 11

Eloquent ORM insert or update

I'm working on a project using Laravel framework’s database component. This library includes a Database Abstraction Layer (DBAL) called “Capsule” and an Object Relational Mapping (ORM) library called “Eloquent”.

I want to update the extension already in DB, but I want also to insert a new "extension" if they don't exist. What is best way to do that?

My code so far:

try {
    $updatedDomainsCount = Capsule::table('tbldomainpricing')
        ->where('extension', $tld)
        ->update(
            [
                'extension' => $tld,
                'autoreg' => 'domenytv',
                'grace_period' => '15',

                 'idprotection' => $idp,
                  'grace_period_fee' => $rea_price,
            ]
        );

    echo "Updated {$updatedDomainsCount} TLD: $tld</br>";
} catch (\Exception $e) {
    echo "I couldn't update domains tld. {$e->getMessage()}";
}   

Upvotes: 1

Views: 8828

Answers (1)

engrhussainahmad
engrhussainahmad

Reputation: 1134

Use Eloquent ORM method: updateOrCreate, use to create new record or updated existing.

Eloquent documentation

Upvotes: 5

Related Questions