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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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