在线或离线设置

I use Laravel 5.0, I need to set models online/offline. I use a toggle checkbox from bootstrap:

<td>
<input onchange="window.location.href='/changestate'" type="checkbox" checked data-toggle="toggle" data-on="Online" data-off="Offline" data-size="mini" >
</td>

my function in my Controller:

public function changeState($id)
{

if (checked === "Online") {
    $job->confirmed=1;
    $job->save();
} else if (checked ==="Offline") {
        $job->confirmed=0;
        $job->save();
}
return redirect('/');
}

and a route for my function:

Route::get('/changeState/{id}','JobController@changeState');

I set the boolean in my Database True or False / Online or Offline

Could somebody help me?

What this looks like to me, is you are refreshing the page every time you click, but by predefining checked, they will, if the code was to work, always uncheck them in the DB.

You are not passing any $id into the URL therefore job is not loaded. Checked will appear undefined, the variable is not set.

Trying to do it with inline JS is not efficient. I'm going to assume you've loaded jQuery, and if you haven't, I very much suggest you do for the simplicity and easy of sending data.

The following answer is just a basic re-write of what you COULD do to achieve the expected result.

//checkbox

<td>
    <input class="changeStatus" data-modelid="{{ $model->id }}" 
    type="checkbox" {{ ($model->confirmed == 1) ? 'checked' : '' }} 
    data-toggle="toggle" data-on="Online" data-off="Offline" 
    data-size="mini" 
    >
</td>

// js

<script>
$('.changeStatus').change(function() {
    var modelId = $(this).data('modelid');
    var checked_status = $(this).prop('checked');
    var checked;
    if(checked_status == true) checked = 1; else checked = 0;
    $.ajax({
        url: '/changestate/'+modelid+'?checked='+checked,
        type: 'get'
    }); 
});
</script>

// controller

use Illuminate\Http\Request;
public function changeState(Request $request, $id)
{
    if($request->query('checked')) {
        if($request->query('checked') == 1 || $request->query('checked') == 0) {
            $job = Model::find($id);
            $job->confirmed = $request->query('checked');
            $job->save();
        }
    }
}