How To Delete All Records From Table In Laravel ?

10-Mar-2023

.

Admin

Hi Guys,

In this example, I will learn you how to use delete all records from table in laravel.you can easy and simply use delete all records from table in laravel.

Do you require to delete all data from table using laravel eloquent? If yes then you can get a solution for your problem. We can easily remove all records from table using DB class with delete().

But if you need to destroy all records using laravel eloquent model then how you will do it?, Actually i cache my all records and when someone remove that records then automatic remove from cache too. but it is possible if you are doing with laravel eloquent model. So is there method truncate() with eloquent but it is not working for me, so finally i found solution for delete all records from database table using following way.

Example 1:


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

User::truncate();

}

}

Example 2:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

User::whereNotNull('id')->delete();

}

}

Example 3:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

User:where('id', 'like' '%%')->delete();

}

}

Example 4:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

DB::table('users')->delete();

}

}

It will help you...

#Laravel 7

#Laravel

#Laravel 6