How to Delete Multiple Records in Laravel 9 - TechvBlogs

How to Delete Multiple Records in Laravel 9

In this article, You will learn How to Delete Multiple or Single Records in Laravel 9. Also, You can use these methods for any other laravel versions.


Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya
 

3 weeks ago

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 9. Also, you can learn How to delete a single record from the database.

How to Delete Multiple Records in Laravel 9

App\Models\Post.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

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

app\Http\Controllers\PostController.php

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

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;

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 App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;

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.

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

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

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

Comments (0)

ServerAvatar
Multiple sites on a single VPS/VM/Server Deploy PHP sites on cloud in minutes, simple as that!
ServerAvatar
Generate Free SSL Certificate - SSLForWeb

Generate Free SSL Certificate

  • 90-Day Certificates
  • SSL Monitoring
  • Wildcard Certificates
  • Multiple Domains
  • Certificate Management
  • Expiration Reminders

Comment


Note: All Input Fields are required.