使用Laravel为所有ID更新数据库中的列

时间:2019-01-23 02:07:00

标签: sql laravel laravel-5

如何使用Laravel中的数据库查询一次更新表中的所有行(所有ID)?我当前的控制器代码如下:

let startTime;
let stopTime;
let timerStatus = 0;
let timePassed;
let interval;

window.onkeyup = function(event) {
	if(event.keyCode == 32) {
		if(timerStatus == 0) {
			start();
			display();
		} else if(timerStatus == 2) {
			timerStatus = 0;
		}
	}
}

window.onkeydown = function(event) {
	if(event.keyCode == 32) {
		if(timerStatus == 1) {
			stop();
		} else if(timerStatus == 0) {
			$("#time").html("0.00");
		}
	}
}
		
function start() {
	let d = new Date();
	startTime = d.getTime();
	timerStatus = 1;
}

function stop() {
	let d = new Date();
	stopTime = d.getTime();
	clearInterval(interval);
	timerStatus = 2;

}

function display() {
	interval = setInterval(function() {
		let now = Date.now();
		timePassed = now - startTime;

		let seconds = Math.floor(timePassed / 1000);
		let minutes = Math.floor(timePassed / 60000);
		let milliseconds = timePassed % 1000;
		//getting rid of third decimal place
		milliseconds = milliseconds.toString().slice(0, -1);
		parseFloat(milliseconds);
		console.log(milliseconds);
		
		let time = minutes + ":" + seconds + "." + milliseconds;

			if(milliseconds < 10) milliseconds = "0" + milliseconds;	
			if(milliseconds < 1) milliseconds = "0" + milliseconds;
			if(minutes < 1) {
				time = seconds + "." + milliseconds;
			}

		$("#time").html(time);

	}, 10);
}

1 个答案:

答案 0 :(得分:0)

要更新整个表(整个表),您需要执行以下操作(删除条件,即您只想更新id = 1的匹配项):

public function updateSchedule(Request $request, $id)
{
    $timein = $request->input('timeIn');
    $timeout = $request->input('timeOut');

    DB::table('schedules')
        ->update(['time_in' => $timein, 'time_out' => $timeout]);
}

如果您想在以后查看内容,则需要返回该值,视图或重定向到上一页。

例如,以下内容(尽管不知道使用back()的来源是不明智的做法,但最好使用特定的路由/网址)。

public function updateSchedule(Request $request, $id)
{
    $timein = $request->input('timeIn');
    $timeout = $request->input('timeOut');

    DB::table('schedules')
        ->update(['time_in' => $timein, 'time_out' => $timeout]);

    return back();
}