Download the PHP package mosamirzz/bulk-query without Composer
On this page you can find all versions of the php package mosamirzz/bulk-query. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mosamirzz/bulk-query
More information about mosamirzz/bulk-query
Files in mosamirzz/bulk-query
Package bulk-query
Short Description Perform bulk update/insert/delete with laravel.
License MIT
Homepage https://github.com/mosamirzz/laravel-bulk-query
Informations about the package bulk-query
Bulk Query
Perform Bulk/Batch Update/Insert/Delete with laravel.
Problem
I tried to make bulk update with laravel but i found that Laravel doesn't support that; so i created this package through it you can perform Batch/Bulk Update, Insert and delete.
Installation
You can install the package via composer:
Usage
Bulk or batch update meaning: that you can update multiple records in single query. Same for delete and insert (can insert or delete multiple records in single query).
The package provided you with 4 classes:
Insert::classUsed to insert multiple records in single query.Update::classUsed to update multiple records in single query.InsertOrUpdate::classUsed to insert multiple records in single query, and when duplicate record found in the records it will update it.- This query updates the records only when it find a
uniqueorprimary key. meaning that if the table doesn't haveuniqueorprimary keycolumn and tried to insert record twice it will inserted as 2 records and will not update the record. - note: the
primary keysholud sent to the query to determine by it if the query will update the duplicate records or not. if it will not send anduniquecolumn not sent to the query then the query will perfom insert only.
- This query updates the records only when it find a
Delete::classUsed to delete multiple records in single query.
Bulk Delete:
You can perfrom Bulk or batch delete by using the class Delete::class as following:
As shown above the default of delete statment is delete by the column id but if you want to perform the delete query with different column you can use the method useKey and pass to it the column name as following:
The default
keyused in the delete is the columnid
Bulk Insert:
You can perform bulk insert by using the class Insert::class as following:
As shown above the prepare method accepts array[] each array represents the record that we want to insert in the database.
If you are trying to send record that exists before in the table, then the query will throw an exception like the following:
SQLSTATE[23000]: Integrity constraint violation
Bulk Update:
You can perform bulk update by using the class Update::class as following:
As shown above
- the
preparemethod acceptsarray[]each array represents the record that we want to update in the database. - each record must be like
"key" => [...]thekeyrepresent the column name we use to updatte the record. in the example above the key it's default isidso when we want to update the record we must send the recordidas thekeyandarrayof values that we want to update.
You can change the default column id used by the update to another column with useKey method.
for example you can use the email column as the key used by update as the following:
As shown above we used email column as the key used in update statement.
The default
keyused in the update is the columnid
If you have a record that you want to update only one column and set the other columns values to old value in the database then you can remove the key=>value of the column from the array as following:
As shown above the user with id = 1 will update the column is_admin only, we don't need to pass the name and password columns to it, the query will put the old value of name and password for the user with id = 1.
Same for the user with id = 3 the columns that will be updated is name and is_admin but the password column will be the old value.
Also the user with id = 70 only password will be updated.
Bulk Insert or Update:
You can perform bulk insert and update in the same query by using the class InsertOrUpdate::class.
The query updates the record only when it find a
uniquecolumn orprimary keycolumn.
You can use the class as the following:
As shown above
- suppose that the user with
email=[email protected]is exists in the table before. - So when the query executes the record that has
email=[email protected]will be updated but the only columnsnameandpasswordare updated. - The second record that has
email=[email protected]will be inserted to the table because it's not exists before.
Testing
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Mohamed Samir
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.