<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Chakra Timilsina on Medium]]></title>
        <description><![CDATA[Stories by Chakra Timilsina on Medium]]></description>
        <link>https://medium.com/@ChakraTimilsina?source=rss-995d1c134843------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*nYM64vq-l0dojDJsB4cYeQ.jpeg</url>
            <title>Stories by Chakra Timilsina on Medium</title>
            <link>https://medium.com/@ChakraTimilsina?source=rss-995d1c134843------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 23:39:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ChakraTimilsina/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Laravel-Batch Use For Update to Array Data in Laravel]]></title>
            <link>https://medium.com/@ChakraTimilsina/laravel-batch-use-for-update-to-array-data-in-laravel-a53169d30265?source=rss-995d1c134843------2</link>
            <guid isPermaLink="false">https://medium.com/p/a53169d30265</guid>
            <category><![CDATA[laravel-batch]]></category>
            <category><![CDATA[mavinoo]]></category>
            <category><![CDATA[batch]]></category>
            <category><![CDATA[batch-update]]></category>
            <category><![CDATA[multiple-data-updating]]></category>
            <dc:creator><![CDATA[Chakra Timilsina]]></dc:creator>
            <pubDate>Sat, 04 Jun 2022 04:00:23 GMT</pubDate>
            <atom:updated>2023-05-22T14:29:00.665Z</atom:updated>
            <content:encoded><![CDATA[<p>Laravel-batch is a package that is used to update array data in laravel, if you are going to update multiple database records with different values. Laravel supports batch updates because it is easy to mass update in the database.</p><p>If we use the Batch function, there is not necessary to loop in the array while updating your function.</p><p>well let’s start,</p><ol><li>installation :</li></ol><p>Run the under-mentioned command to install Mavinoo/Laravel-batch in Laravel.</p><pre>composer require mavinoo/laravel-batch</pre><p>Open your config/app.php and add the following to the providers array:</p><p>2)Service Provider :</p><p>file app.php in array providers</p><pre>Mavinoo\Batch\BatchServiceProvider::class</pre><p>3)Aliases :</p><p>file app.php in array aliases :</p><pre>&#39;Batch&#39; =&gt; Mavinoo\Batch\BatchFacade::class,</pre><p>For Eloquent</p><p>Batch is easy for eloquent methods. There is no need for any customized code in your batch.php. so you can try this method.</p><p>In controller,</p><pre>use App\Models\Profile;<br>use Batch;</pre><p>After that, visit app/Http/controllers and open the ProfileController.php file. And you can use this method to update Array Data:</p><pre>$profile = new Profile;<br>$value = [<br>[<br>&#39;id&#39; =&gt; 1,<br>&#39;name&#39; =&gt; &#39;Chakra Timilsina&#39;,<br>&#39;address&#39; =&gt; &#39;Kailali&#39;<br>] ,<br>[<br>&#39;id&#39; =&gt; 2,<br>&#39;name&#39; =&gt; &#39;Angelika Carini&#39;,<br>&#39;address&#39; =&gt; &#39;Lalitpur&#39;<br>] ,<br>[<br>&#39;id&#39; =&gt; 3,<br>&#39;name&#39; =&gt; &#39;Roxanne Glenn&#39;,<br>&#39;address&#39; =&gt; &#39;Butwal&#39;<br>] ,</pre><pre>];<br>$index = &#39;id&#39;;<br>Batch::update($profile, $value, $index);</pre><p>For Query Builders</p><p>If Query Builders are used in your function, you need to change or customize the code. You can try this code for the update case.</p><p>vender/mavinoo/laravel-batch/src/batch.php</p><pre>public function update(Model $table, array $values, string $index = null, bool $raw = false)<br>{</pre><pre>$final = [];<br>$ids = [];<br>if (!count($values)) {<br>return false;<br>}<br>if (!isset($index) || empty($index)) {<br>$index = $table-&gt;getKeyName();<br>}<br>$connection = config(&#39;database.default&#39;);<br>$driver = config(&quot;database.connections.{$connection}.driver&quot;);<br>foreach ($values as $key =&gt; $val) {<br>$ids[] = $val[$index];<br>if ($table-&gt;usesTimestamps()) {<br>$updatedAtColumn = $table-&gt;getUpdatedAtColumn();<br>if (!isset($val[$updatedAtColumn])) {<br>$val[$updatedAtColumn] = Carbon::now()-&gt;format($table-&gt;getDateFormat());<br>}<br>}</pre><pre>foreach (array_keys($val) as $field) {<br>if ($field !== $index) {<br>// If increment / decrement<br>if (gettype($val[$field]) == &#39;array&#39;) {<br>// If array has two values<br>if (!array_key_exists(0, $val[$field]) || !array_key_exists(1, $val[$field])) {<br>throw new \ArgumentCountError(&#39;Increment/Decrement array needs to have 2 values, a math operator (+, -, *, /, %) and a number&#39;);<br>}<br>// Check first value<br>if (gettype($val[$field][0]) != &#39;string&#39; || !in_array($val[$field][0], [&#39;+&#39;, &#39;-&#39;, &#39;*&#39;, &#39;/&#39;, &#39;%&#39;])) {<br>throw new \TypeError(&#39;First value in Increment/Decrement array needs to be a string and a math operator (+, -, *, /, %)&#39;);<br>}<br>// Check second value<br>if (!is_numeric($val[$field][1])) {<br>throw new \TypeError(&#39;Second value in Increment/Decrement array needs to be numeric&#39;);<br>}<br>// Increment / decrement<br>$value = &#39;`&#39; . $field . &#39;`&#39; . $val[$field][0] . $val[$field][1];<br>} else {<br>// Only update<br>$finalField = $raw ? Common::mysql_escape($val[$field]) : &quot;&#39;&quot; . Common::mysql_escape($val[$field]) . &quot;&#39;&quot;;<br>$value = (is_null($val[$field]) ? &#39;NULL&#39; : $finalField);<br>}<br>if (Common::disableBacktick($driver))<br>$final[$field][] = &#39;WHEN &#39; . $index . &#39; = \&#39;&#39; . $val[$index] . &#39;\&#39; THEN &#39; . $value . &#39; &#39;;<br>else<br>$final[$field][] = &#39;WHEN `&#39; . $index . &#39;` = \&#39;&#39; . $val[$index] . &#39;\&#39; THEN &#39; . $value . &#39; &#39;;<br>}<br>}<br>}<br>if (Common::disableBacktick($driver)) {<br>$cases = &#39;&#39;;<br>foreach ($final as $k =&gt; $v) {<br>$cases .= &#39;&quot;&#39; . $k . &#39;&quot; = (CASE &#39; . implode(&quot;\n&quot;, $v) . &quot;\n&quot;<br>. &#39;ELSE &quot;&#39; . $k . &#39;&quot; END), &#39;;<br>}<br>$query = &quot;UPDATE \&quot;&quot; . $this-&gt;getFullTableName($table) . &#39;&quot; SET &#39; . substr($cases, 0, -2) . &quot; WHERE \&quot;$index\&quot; IN(&#39;&quot; . implode(&quot;&#39;,&#39;&quot;, $ids) . &quot;&#39;);&quot;;<br>} else {<br>$cases = &#39;&#39;;<br>foreach ($final as $k =&gt; $v) {<br>$cases .= &#39;`&#39; . $k . &#39;` = (CASE &#39; . implode(&quot;\n&quot;, $v) . &quot;\n&quot;<br>. &#39;ELSE `&#39; . $k . &#39;` END), &#39;;<br>}<br>$query = &quot;UPDATE `&quot; . $this-&gt;getFullTableName($table) . &quot;` SET &quot; . substr($cases, 0, -2) . &quot; WHERE `$index` IN(&quot; . &#39;&quot;&#39; . implode(&#39;&quot;,&quot;&#39;, $ids) . &#39;&quot;&#39; . &quot;);&quot;;<br>}<br>return $this-&gt;db-&gt;connection($this-&gt;getConnectionName($table))-&gt;update($query);<br>}</pre><p>In controller,</p><pre>use Batch;</pre><p>After that, visit app/Http/controllers and open the ProfileController.php file. And you can use this method to update Array Data:</p><pre>$value = [<br>[<br>&#39;id&#39; =&gt; 1,<br>&#39;name&#39; =&gt; &#39;chakra timilsina&#39;,<br>&#39;address&#39; =&gt; &#39;kailali&#39;<br>] ,<br>[<br>&#39;id&#39; =&gt; 2,<br>&#39;name&#39; =&gt; &#39;Angelika Carini&#39;,<br>&#39;address&#39; =&gt; &#39;lalitpur&#39;<br>] ,<br>[<br>&#39;id&#39; =&gt; 3,<br>&#39;name&#39; =&gt; &#39;Roxanne Glenn&#39;,<br>&#39;address&#39; =&gt; &#39;butwal&#39;<br>] ,<br>];</pre><pre>$index = &#39;id&#39;;<br>$table=&#39;profile&#39;;</pre><pre>Batch::update($table, $value, $index);<br>or<br>Batch::update(&#39;profile&#39;,$value,&#39;id&#39;);</pre><p>I’m thankful to <a href="https://github.com/mavinoo">Mavinoo</a> for this nice and simple package.</p><p>I hope you get an idea about <strong>Batch updating multiple records using Laravel</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a53169d30265" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>