How to Delete Multiple Records in Laravel 10

How to Delete Multiple Records in Laravel 10

In this post we will give you information about How to Delete Multiple Records in Laravel 10. Hear we will give you detail about How to Delete Multiple Records in Laravel 10 And how to use it also give you demo for it if it is necessary.

Hey Folks, It almost needs to remove multiple records. If you are developing e-commerce or any extensive web application, you may be provided an option to delete multiple records.

In this article, You will learn How to Delete Multiple Records in Laravel 10. Also, you can learn How to delete a single record from the database.

How to Delete Multiple Records in Laravel 10

AppModelsPost.php

<?php

namespace AppModels;

use IlluminateDatabaseEloquentModel;

class Post extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'posts';

    protected $fillable = ['title', 'banner', 'slug', 'description'];
}

appHttpControllersPostController.php

You can use the destroyMultiple method and pass one or more primary keys to it as a form request.

<?php

namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsPost;

class PostController extends Controller
{        
    public function destroyMultiple(Request $request){
        try {

            Post::destroy($request->ids);
            return response()->json([
                'message'=>"Posts Deleted successfully."
            ],200);

        } catch(Exception $e) {
            report($e);
        }
    }
}

You can also use this option to remove records with a custom parameter.

Post::where('title', 'Laravel')->delete();

Also, you can use the query builder to delete multiple and single records from the database. Here is an example of Eloquent Query for deleting single or multiple records from the database in Laravel.

<?php

namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsPost;

class PostController extends Controller
{        
    public function destroyMultiple(Request $request){
        try {

            DB::table('posts')->whereIn('id',$request->ids)->delete();
            return response()->json([
                'message'=>"Posts Deleted successfully."
            ],200);

        } catch(Exception $e) {
            report($e);
        }
    }
}

Thank you for reading this blog.

Also see: How to Get only records created today in Laravel

  .       .

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

Hope this code and post will helped you for implement How to Delete Multiple Records in Laravel 10. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US