ayushflitzip :
Can you guys help me solve my issue? How To Delete All Records From Database All Tables in a single click using Laravel ajax. I have to delete all records from all tables, please help to solve my issue.
Thanks in advance
Vinay Kaklotar :
You should try this
In ajax
$(document).on('.delete-all-record', function (e) {
e.preventDefault();
$.ajax({
type: 'GET',
url: 'url',
success: function (data) {
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert("Some problem occurred, please try again.");
}
});
});
In Controller
$tableNames = DB::select('SHOW TABLES');
foreach ($tableNames as $name) {
//if you don't want to truncate migrations
if ($name->Tables_in_db_name == 'migrations') {
continue;
}
if ($name->Tables_in_db_name == 'users') {
continue;
}
DB::table($name->Tables_in_db_name)->truncate();
}