1

All of my data are stored on a multidimensional array and I want to use laravel update with only one line of code without using any loop on every update.

If there is a code like this when inserting multiple data in laravel.

$applicants_insert_data = [[
     'id' => 1,
     'name' => 'sample1',
     'contact' => '09123456789',
     'email' => [email protected],
],[
     'id' => 1,
     'name' => 'sample2',
     'contact' => '09987654321',
     'email' => [email protected],
]]; 

applicants::insert($applicants_insert_data);

Is it possible or any way that I can use that kind of method in updating my data?

like this:

$applicants_update_data = [[
     'name' => 'sample1',
     'contact' => '09123456789',
     'email' => [email protected],
],[
     'name' => 'sample2',
     'contact' => '09987654321',
     'email' => [email protected],
]]; 

$applicants_id = [[
     'id' => 1
],[
     'id' => 2,
]];

applicants::find($applicants_id)->update($applicants_update_data);

1 Answer 1

3

As far as I know this is not available out of the box in Laravel. However, there is a third party package that allows this type of functionality:

https://packagist.org/packages/mavinoo/laravel-batch

From the examples posted in the docs:

$table = 'users';

$value = [
     [
         'id' => 1,
         'status' => 'active',
         'nickname' => 'Mohammad'
     ] ,
     [
         'id' => 5,
         'status' => 'deactive',
         'nickname' => 'Ghanbari'
     ] ,
];

$index = 'id';


Batch::update($table, $value, $index);
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks! I'll Check it right away. I'll update you If there is any prob.
Hi Jiromas. That looks like a great suggestion by Peter however if you look at the code it loops through array of values.
Looks to me like the loop is just building up the single SQL string of values, then it gets executed in one query.
@Peter Hi! I just tested it and it worked perfectly as I want to.
Glad to hear it!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.