PHP code example of yadakhov / insert-on-duplicate-key

1. Go to this page and download the library: Download yadakhov/insert-on-duplicate-key library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yadakhov / insert-on-duplicate-key example snippets


use Illuminate\Database\Eloquent\Model;
use Yadakhov\InsertOnDuplicateKey;

/**
 * Class User.
 */
class User extends Model
{
    // The function is implemented as a trait.
    use InsertOnDuplicateKey;
}

    $users = [
        ['id' => 1, 'email' => '[email protected]', 'name' => 'User One'],
        ['id' => 2, 'email' => '[email protected]', 'name' => 'User Two'],
        ['id' => 3, 'email' => '[email protected]', 'name' => 'User Three'],
    ];

    $users = [
        ['id' => 1, 'email' => '[email protected]', 'name' => 'User One'],
        ['email' => '[email protected]', 'id' => 2, 'name' => 'User Two'],
        ['email' => '[email protected]', 'name' => 'User Three', 'id' => 3],
    ];

    User::insertOnDuplicateKey($users);

    User::insertOnDuplicateKey($users, ['email']);

    $users = [
        ['id' => 1, 'name' => 'User One', 'heritage' => 1000],
        ['id' => 2, 'name' => 'User Two', 'heritage' => 2000],
        ['id' => 3, 'name' => 'User Three', 'heritage' => 1500],
    ];

    User::insertOnDuplicateKey($users, ['heritage' => DB::raw('`heritage` + VALUES(`heritage`)')]);

    User::insertIgnore($users);

    User::replace($users);

['id' => 1, 'email' => '[email protected]', 'name' => 'User One', 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()]