Giter Site home page Giter Site logo

laravel-eloquent-batch's Introduction

Laravel Eloquent BATCH

Build Status

provide an util for batch Insert/update, also an eloquent collection macro batchSave for batch action more convenient in Laravel.

Install

composer require free3_5man/laravel-eloquent-batch

Service Provider

file app.php in array providers :

Freeman\LaravelBatch\BatchServiceProvider::class,

Features

  • make the insert data items assoc with keys, which has the same data structure with update data
  • return insert ids when primary key is integer auto_increment
  • MySQL, PostgreSQL, SQLite and SQL Server is supported
  • json/jsonb field is supported
  • auto chunked when generate batch sql
  • support Eloqunent Collection macro batchSave for auto batchInsert and batchUpdate
  • batchSave fill items with insert id when actually insert and primary key is integer auto_increment
  • batchSave return updated models when actually update
  • batchSave can save inserts and updates at the same time
  • batchSave can control whether to touch updated_at with the param $touchUpdatedAt

Batch Insert Example

use App\Models\Player;
use Freeman\LaravelBatch\BatchUtil;

$data = [
    // this is the first item, columns of this item will be used as insert columns
    // if columns of any other items does not equal to this, false will be returned as result 
    // all the columns of each item required to be same
    [
        'name' => 'kaka',
        'birthday' => '1982-04-22',
    ],
    [
        'name' => 'nesta',
        'birthday' => '1976-03-19',
    ],
    [
        'name' => 'pirlo',
        'birthday' => '1979-05-19',
    ],
];
// batchInsert use the first item columns as insert columns default;
$ids = BatchUtil::ofModel(new Player)->batchInsert($data);
// if players primay key auto_increment, insert ids array ([1, 2, 3]) will be returned, else empty array ([]) will be returned

$data[1]['is_captain'] = true;
BatchUtil::ofModel(new Player)->batchInsert($data); // return false

Batch Update Example

$data = [
    [
        'id' => 1,
        'name' => 'kaka',
        'birthday' => '1982-04-22',
    ],
    [
        'id' => 2,
        'name' => 'nesta',
        'birthday' => '1976-03-19',
    ],
    [
        'id' => 3,
        'name' => 'pirlo',
        'birthday' => '1979-05-19',
    ],
];
BatchUtil::ofModel(new Player)->batchUpdate($data);

batchSave Example

$data = [
    [
        'name' => 'kaka',
        'birthday' => '1982-04-22',
    ],
    [
        'name' => 'nesta',
        'birthday' => '1976-03-19',
    ],
    [
        'name' => 'pirlo',
        'birthday' => '1979-05-19',
    ],
];
// batchSave will do batch insert here
$players = collect($data)->map(function($item) {
    return new Player($item);
})->batchSave();    // each player has id
dump($players->pluck('id')->toArray()); // [1, 2, 3]

// batchSave will do batch update here, updated_at will be touched automaticly
$players->map(function($player) {
    return $player->fill([
        'name' => $player->name . '-' . $player->id,
    ]);
})->batchSave();
dump($players->pluck('name')->toArray());   // ['kaka-1', 'nesta-2', 'pirlo-3']

Testing

$ composer test

laravel-eloquent-batch's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.