Download the PHP package lapaliv/laravel-bulk-upsert without Composer

On this page you can find all versions of the php package lapaliv/laravel-bulk-upsert. 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-bulk-upsert

Laravel Bulk Upsert with eloquent's events

Annotation

How often do you need to insert a collection of models? I have this task quite often. Laravel has a solution for mass insert/update/upsert, but it uses a different algorithm than eloquent. For example, when we are using $model->create() we can supplement the data in the observers, and it doesn't work when we use Model::query()->insert().

The second problem is in the number of fields. We need to align the fields before passing them to insert/update/upsert method of the builder. This is not always convenient.

The third problem is in the way get the inserted rows back after inserting. Laravel doesn't return them. Of course, it won't be a big deal if you have only one unique column, but you will need some time to write quite large SQL query to select them in another case.

Because of the above I have written this library which solves these problems. Using this library you can save a collection of your models and use eloquent events such as creating/created, updating/updated, saving/saved, deleting/deleted, restoring/restored, forceDeleting/forceDeleted at the same time. And you don't need to prepare the number of fields before.

In simple terms, this library runs something like this:

but with a few queries to the database per chunk.

Features

Documentation for version 1.x

The documentation for version 1.x you can see here

Requires

Installation

You can install the package via composer:

Get started

Use the trait Lapaliv\BulkUpsert\Bulkable in your model(s):

Usage

Make the instance

There are four ways how you can make an instance

Creating / Inserting

Preparing the data

You can just create these users.

You can create and get back these users.

You can accumulate rows until there are enough of them to be written.

Updating

Preparing the data

You can just update these users.

You can update these users and get back a collection of the models.

You can accumulate rows until there are enough of them to be written.

Extra way

There is an extra way how you can update your data in the database:

This way loads the data from database and updates found rows by the query.

Upserting (Updating & Inserting)

Preparing the data

You can just upsert these users.

You can upsert these users and get back a collection of the models.

You also can accumulate rows until there are enough of them to be written.

Force/Soft Deleting (since v2.1.0)

Preparing the data

You can just delete these users. If your model uses the trait Illuminate\Database\Eloquent\SoftDeletes, then your model will delete softly else force.

Or you can force delete them.

You also can accumulate rows until there are enough of them to be deleted.

Listeners

The order of events

The order of calling callbacks is:

How listen to events

There are three ways how you can listen events from the library:

How listen to events: Bulk Callbacks

How listen to events: Model Callbacks

You also can use model callbacks. They are almost the same. Just remove the prefix on. For example:

How listen to events: Observer

You also can use observers. For example:

Example

You can observe the process. The library supports the base eloquent's events and some extra which are give you the access to the collection of models. Listeners with collections accept extra parameter with type BulkRows. This is a collection of class Lapaliv\BulkUpsert\Entities\BulkRow which contains your original data (the property original), the model (the property model) and the unique attributes which were used for saving (the property unique). It can help you to continue your saving if, for example, you had some relations in your data.

Let's look at the example:

In this example you have a chain. After upserting the user, the library will run UserObserver::savedMany() where the code will prepare comments and will upsert them.

API

TODO

Tests

You can check the actions or run it on your laptop:


All versions of laravel-bulk-upsert with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
nesbot/carbon Version ^2.0
illuminate/database Version *
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 lapaliv/laravel-bulk-upsert contains the following files

Loading the files please wait ....