Download the PHP package mavinoo/laravel-batch without Composer

On this page you can find all versions of the php package mavinoo/laravel-batch. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-batch

Laravel BATCH (BULK)

Bulk database operations for Laravel: update, insert, upsert, delete, sync, import and export many rows in a few queries, on MySQL, MariaDB, PostgreSQL and SQLite.

Tests License Latest Stable Version Total Downloads Daily Downloads

What it does

Requirements

laravel-batch PHP Laravel Databases
3.x 8.1 – 8.5 10 – 13 MySQL, MariaDB, PostgreSQL, SQLite
2.5.x 7.1 – 8.5 not pinned (tested 8, 11) MySQL, MariaDB (PostgreSQL and SQLite only partly)

Upgrading from 2.x? Read UPGRADE.md.

Install

The service provider and the Batch facade are registered automatically.

Update

Each row holds the index value (here id) and the columns to set. Rows can set different columns.

The index defaults to the model's primary key. update() returns the number of affected rows. MySQL and MariaDB only count rows whose values really changed; PostgreSQL and SQLite count every matched row.

If the same index value appears in more than one row of a call, only the first of those rows is applied.

Values can be strings, numbers, booleans, null, dates and enums (backed enums are stored by their value, other enums by their name).

Increment / decrement

Raw SQL values

Wrap a value in DB::raw() to use it as SQL instead of as a string:

Never put user input inside DB::raw(). Every other value is sent as a bound parameter and is safe.

JSON columns

Set keys inside a JSON column with ->, like Laravel's update():

For columns the model casts to JSON (array, json, object, collection, AsArrayObject, AsCollection, ...), arrays and collections are encoded the way the model would store them, in every method:

Without such a cast, an array value is read as an increment / decrement.

Update with two index columns

Rows are matched on both columns:

Update with any number of key columns

Rows are matched on every column in the third argument:

Update with different conditions per row

Each item has its own conditions and columns. Every item's conditions must include the key column (the third argument, the primary key by default).

A condition column can be updated in the same call, as status is above. On MySQL and MariaDB, two columns that are both updated and used in each other's conditions can't be updated in one call, because MySQL assigns columns one after another. The call throws an InvalidArgumentException; split it into two calls.

updated_at

If the model uses timestamps, updated_at is set to the current time only for rows where a value actually changes. Pass updated_at in a row to set it yourself.

Insert

Insert rows given as column => value pairs

insertRows() takes the same options as insert(), but each row names its columns. Every row needs the same columns; their order doesn't matter.

Insert and get the new ids

insertGetIds() takes the same rows as insertRows() and returns the new auto-increment ids, in the same order as the rows:

Insert and report the skipped rows

insertOrIgnoreRows() inserts rows, skips the ones that hit a unique key, like insertOrIgnore(), and tells you exactly which ones were skipped:

Upsert

Insert rows, or update them when a row with the same $uniqueBy values already exists, in one query:

Counters

Combine the stored value with the new one instead of replacing it, for stock levels, totals or high scores:

['+'] adds, ['-'] subtracts, ['max'] keeps the larger value and ['min'] the smaller one. A stored NULL counts as 0 for + and -, and is replaced for max and min. Counter and plain columns can be mixed: ['name', 'qty' => ['+']].

Update only when the new row wins

onlyIf updates an existing row only when the new value compares that way with the stored one, or the stored one is NULL. New rows are always inserted:

The operators are >, >=, <, <= and <>. Several columns must all match. On MySQL and MariaDB at most one onlyIf column can also be updated.

Get the rows back

upsertReturning() takes the same arguments as upsert(), plus the columns to return, and gives back every row of the batch as it is stored afterwards, in the order of the input:

The rows are read back by their uniqueBy values in the same transaction, so this works the same on every database, MySQL included. Rows an onlyIf condition left alone are returned as stored.

Delete

Delete rows matched on one or more key columns:

Models that use SoftDeletes are soft deleted (only deleted_at, and updated_at, are set). Pass true as the fourth argument to delete them for real.

Sync a table with a list

sync() makes the rows a query matches look like a list, in one transaction: rows that are new are inserted, rows that exist are updated, and rows the list doesn't have are deleted.

Import from a file or any iterable

import() reads rows one at a time and writes them a chunk at a time, so files of any size are imported with constant memory. Importing a 500,000-row CSV file used 7 MB of memory instead of the 765 MB that reading it into an array for insertRows() took, and was 30 to 45% faster.

The source can be:

Option Default
mode 'insert' 'insert', 'insertIgnore' or 'upsert'
uniqueBy, update the upsert() arguments, for 'upsert'
chunk 1000 rows per write
atomic true one transaction for the whole import; false commits each chunk
format from the extension 'csv', 'tsv' or 'jsonl'
header true the first CSV row names the columns
columns column names for a CSV file without a header
delimiter, enclosure , (tab for TSV), "
map [source column => table column]
nullValues [] strings stored as null, e.g. ['', 'NULL']
transform fn (array $row, int $number): ?array
onChunk fn (int $written), after every chunk
onError fn (InvalidArgumentException $e, int $number): skip malformed rows instead of stopping
rules Laravel validation rules for each row; failing rows are skipped and reported
fast false load with COPY (PostgreSQL) or LOAD DATA LOCAL INFILE (MySQL, MariaDB)

Validate rows

Rows are checked with Laravel's validator after map and nullValues, and before transform. Rows that fail are skipped and their messages are returned by row number:

Fast imports

'fast' => true hands each chunk to the database's own bulk loader: COPY on PostgreSQL and LOAD DATA LOCAL INFILE on MySQL and MariaDB. Importing a 1,000,000-row CSV file took 6.6 s instead of 18.7 s on PostgreSQL, 8.4 s instead of 18.7 s on MySQL and 6.4 s instead of 12.6 s on MariaDB.

Split a large file

Export

export() writes the rows a query matches to a CSV, TSV or JSON Lines file, also gzipped, a chunk at a time with constant memory:

Option Default
columns the query's select, or every column the columns to export
format from the extension 'csv', 'tsv' or 'jsonl'
header true write the column names first in CSV files
delimiter, enclosure , (tab for TSV), "
maxRows rows per file; the parts are numbered
chunk 1000 rows per query
overwrite false replace existing files instead of throwing
transform fn (array $row): ?array; null leaves the row out
onChunk fn (int $written), after every chunk

Rows are read in primary key order (DB::table() queries use id), so the query can't have a limit or offset. CSV files are written the RFC 4180 way and can be imported again with import().

Large deletes, updates and archives

Deleting or updating millions of rows in one statement locks the table for a long time and fills the undo log. These methods do it a chunk at a time. Rows are walked in primary key order (WHERE id > ? ORDER BY id LIMIT ?), which works on every database and always ends, even when the update stops rows matching the query.

The same methods are available on every Eloquent and query builder:

Turn timestamps off

Laravel's withoutTimestamps() works with every Batch method:

Preview the SQL without running it

pretend() returns the statements Batch would run, with their bindings. Nothing is executed and no database connection is opened.

Calls made inside the callback through the facade, batch() or the HasBatch trait are recorded too.

From a model

Add the HasBatch trait:

The query-based methods are available on every Eloquent query, with or without the trait:

(new User)->updateMultipleCondition() still works, but is deprecated in favour of the static User::batchUpdateMultipleCondition() and will be removed in 4.0.

Helper

Large batches and errors

Tests

The tests run on their own, from the root of this package:

They use an in-memory SQLite database by default. To run them against MySQL, MariaDB or PostgreSQL, create an empty batch_test database and set DB_CONNECTION (plus DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD or DB_SOCKET as needed):

On MySQL the fast-import tests turn local_infile on with SET GLOBAL, so the MySQL user needs that privilege (root has it).

Donate

USDT (BSC) Address: 0xe848f4a94adb70aba2f2da92181096b18aeb269b


All versions of laravel-batch with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
illuminate/database Version ^10.0|^11.0|^12.0|^13.0
illuminate/support Version ^10.0|^11.0|^12.0|^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mavinoo/laravel-batch contains the following files

Loading the files please wait ...