简体   繁体   English

如何从数据库所有表中删除所有记录

[英]How To Delete All Records From Database All Tables

Can you guys help me solve my issue?你们能帮我解决我的问题吗? How To Delete All Records From Database All Tables in a single click using Laravel ajax.如何使用 Laravel ajax 在一次单击中从数据库所有表中删除所有记录。 I have to delete all records from all tables, please help to solve my issue.我必须从所有表中删除所有记录,请帮助解决我的问题。

Thanks in advance提前致谢

( https://i.stack.imgur.com/gu6Qg.png ) ( https://i.stack.imgur.com/gu6Qg.png )

You can make use of the truncate method, this works for Laravel 4 and 5:您可以使用 truncate 方法,这适用于 Laravel 4 和 5:

MyModel::truncate();

It will delete your all data for your single table.它将删除您的单个表的所有数据。

php artisan migrate:refresh

It will delete your all table's all data .it will refresh your all table's data它将删除您所有表的所有数据。它将刷新您所有表的数据

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();
}

using Artisan::call() create function in your controller在控制器中使用Artisan::call()创建函数

use Artisan;

public function refreshDatabase(){
   Artisan::call('migrate:refresh');
   return 'Success';
}

Route::get('refresh', 'YourController@refreshDatabase');

$.ajax('/refresh',   
{
    success: function (data) {
           // you can write
    }
});

EDITED已编辑

in your web.php file use like below without using controller function,在您的web.php文件中使用如下所示而不使用控制器功能,

if you really want use controller function then change function name如果您真的想使用控制器功能,请更改功能名称

    use Artisan;
    Route::get('refresh-tables',function(){
       Artisan::call('migrate:refresh');
           return 'Success';
    });

$.ajax('/refresh-tables',   
    {
        success: function (data) {
               // you can write
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM