Delete All Records from Table in Laravel Eloquent
Now, let's see article of laravel delete all rows from table. We will use laravel delete all records from table. you can understand a concept of laravel delete records from table. we will help you to give example of how to delete all records from table in laravel.
Let's get started with delete all records from table in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.
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.
Let's see bellow examples:
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();
}
}
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent inRandomOrder() Method Example
- Laravel Eloquent whereNotBetween() Query Example
- Laravel Eloquent selectRaw() Query Example
- Laravel Eloquent whereRaw Condition Example
- How to Group By with Order By Desc in Laravel?
- Laravel Order By Relation Column Example
- Laravel Eloquent WhereNotIn Query Example
- How to Create and Use Query Scope in Laravel Eloquent
- How to use Union Query with Laravel Eloquent?
- Laravel Has Many Through Eloquent Relationship Tutorial
- Laravel Where Clause with date_format() Example