如何使用 Laravel 查询生成器在 5 秒后从 table 中删除所有数据?
How to delete all the data from a table after 5 seconds using Laravel Query Builder?
我有这个问题
$data = DB::table('incidencias')
->select(DB::raw("STR_TO_DATE(fecha_notif, '%d/%m/%Y %H:%i:%s') as time"))
->get();
$data = json_decode(json_encode($data, true), true);
DB::table('incidencia_tiempo')->insert($data);
我想在 运行 查询后仅删除一次 table 中的所有数据....例如,如果我刷新,数据将在 5 秒后删除,一次就够了。。。可以吗?
要删除table中的所有数据,就这么简单
DB::table('incidencias')->truncate()
或
DB::table('incidencias')->delete();
更新
如果你想在5秒后删除数据,那么你可以这样做:
sleep(5)
DB::table('incidencias')->delete();
刷新使用Javascript
您的控制器:
public function deleteAll()
{
DB::table('incidencias')->truncate();
if (request()->ajax()) {
return response()->json(['success' => true]);
}
return back(); // if not ajax...
}
你的HTML
// For example, deleting it all every 5 seconds AFTER the delete button is pressed
<button id="delete">Delete all</button>
<script>
$('#delete').click(function () {
// in this case start deleting when you click this delete button only once..
startDeleting();
});
// the actual code that does the GET request to the URL every 5 seconds
function startDeleting() {
setInterval(function () {
// Submit as post/get depending on your route config.
$.ajax({
url: "/your-url-to-controller", success: function (result) {
// what to do if we get success (if you wanna do anything)
console.log(result);
}
});
}, 5000); // time in milliseconds
}
</script>
我有这个问题
$data = DB::table('incidencias')
->select(DB::raw("STR_TO_DATE(fecha_notif, '%d/%m/%Y %H:%i:%s') as time"))
->get();
$data = json_decode(json_encode($data, true), true);
DB::table('incidencia_tiempo')->insert($data);
我想在 运行 查询后仅删除一次 table 中的所有数据....例如,如果我刷新,数据将在 5 秒后删除,一次就够了。。。可以吗?
要删除table中的所有数据,就这么简单
DB::table('incidencias')->truncate()
或
DB::table('incidencias')->delete();
更新
如果你想在5秒后删除数据,那么你可以这样做:
sleep(5)
DB::table('incidencias')->delete();
刷新使用Javascript
您的控制器:
public function deleteAll()
{
DB::table('incidencias')->truncate();
if (request()->ajax()) {
return response()->json(['success' => true]);
}
return back(); // if not ajax...
}
你的HTML
// For example, deleting it all every 5 seconds AFTER the delete button is pressed
<button id="delete">Delete all</button>
<script>
$('#delete').click(function () {
// in this case start deleting when you click this delete button only once..
startDeleting();
});
// the actual code that does the GET request to the URL every 5 seconds
function startDeleting() {
setInterval(function () {
// Submit as post/get depending on your route config.
$.ajax({
url: "/your-url-to-controller", success: function (result) {
// what to do if we get success (if you wanna do anything)
console.log(result);
}
});
}, 5000); // time in milliseconds
}
</script>