Laravel Update Multiple Records

Laravel Update Multiple Records

Update multiple records or columns in laravel; Through this tutorial, we will learn how to update multiple records or columns using ids, types, or different values in laravel.

In laravel app, we have to update many records. So there is a lot of eloquent available in laravel for this. How can we update multiple records or columns by using where(), whereIn() and update() eloquent in laravel.

How to Update Multiple Records in Laravel

There are 3 simple ways to update multiple records or columns in laravel apps:

  • 1 Method – Update Multiple Record with Where()
  • 2 Method – Update Multiple Record with WhereIn using Ids
  • 3 Method – Update Multiple Record with WhereIn() using Request Data

1 Method – Update Multiple Record with Where()

Using this method, we can update multiple records or columns with where() eloquent in laravel:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Post;
  
class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        Post::where("status", "publish")
                ->update(["status" => "draft"]);
  
        dd("Post updated successfully.");
    }
}

2 Method – Update Multiple Record with WhereIn using Ids

Using this method, we can update multiple records with whereIn() eloquent using ids in laravel:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Post;
  
class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $ids = [40, 60, 15, 110];
  
        Post::whereIn("id", $ids)
                ->update([
                    'status' => 'draft',
                    'content' => 'this is first post'
                ]);
  
        dd("Posts updated successfully.");
    }
}

3 Method – Update Multiple Record with WhereIn() using Request Data

Using this method, we can update records or columns using requested data in laravel:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Post;
  
class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $ids = [40, 50, 55, 110];
  
        Post::whereIn("id", $ids)
                ->update($request->all());
  
        dd("Posts updated successfully.");
    }
}

Conclusion

Through this tutorial, we have learned how to update multiple records or columns using ids, types, array or different values in laravel.

Recommended Laravel Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *