Laravel:如果为真或假,请选中复选框,然后更新数据库


Laravel: Check checkbox if true or false, then update database

刚开始使用phpStorm的laravel。我试图创建一个接受输入和复选框的表单,然后将其添加到数据库中。数据库中的所有行都列在视图索引.html中。应允许用户激活或停用(使用复选框)浏览 index.html 文件时应列出的消息。因为如果一篇新闻文章处于活动状态,并且您想停用它,您应该能够取消选中该复选框,它就会被停用。所以我想知道我的函数应该是什么样子。

我也不确定如何在选中或取消选中复选框时运行功能激活或停用。

这是我的第一篇文章,所以请告诉我的描述中是否有难以理解的内容。

我的路线:

Route::get('admin',
    [
        'uses' => 'NyhetsController@index',
        'as' => 'admin.index'
    ]
);

Route::get('admin/create',
    [
        'uses' => 'NyhetsController@create',
        'as' => 'admin.create'
    ]
);
Route::post('admin',
    [
        'uses' => 'NyhetsController@store',
        'as' => 'admin.store'
    ]
);
Route::get('admin/{id}',
    [
        'uses' => 'NyhetsController@show',
        'as' => 'admin.show'
    ]
);
Route::get('admin/{id}/edit',
    [
        'uses' => 'NyhetsController@edit',
        'as' => 'admin.edit'
    ]
);
Route::put('admin/{id}',
    [
        'uses' => 'NyhetsController@update',
        'as' => 'admin.update'
    ]
);
Route::get('admin/{id}/delete',
    [
        'uses' => 'NyhetsController@delete',
        'as' => 'admin.delete'
    ]
);
Route::delete('admin/{id}',
    [
        'uses' => 'NyhetsController@destroy',
        'as' => 'admin.destroy'
    ]
);

我的控制器包含该函数(只是我在愤怒中写的东西):

public function ActivatedOrDeactivated($id){
$news = News::findOrFail($id);
$input = Input::get('active');
if($input == true)
{
    $news->active = 'false';
    $news->update($input);
}
else{
    $news->active = 'true';
    $news->update($input);
}
}

我的索引文件包含以下格式:

<h1>Alle postene i databasen</h1>
<p>{{ link_to_route('admin.create', 'Legg til ny nyhet') }}</p>

@if($news->count())
    <table class="table table-striped table-boarded">
        <thead>
            <tr>ID</tr>
            <tr>Title</tr>
            <tr>Author</tr>
            <tr>Message</tr>
            <tr>Active</tr>
            <tr>Picture path</tr>
            <tr>Activate/Deactivate</tr>
        </thead>
        <tbody>
            @foreach($news as $n)
                <tr>
                    <td>{{ $n->id }}</td>
                    <td>{{ $n->title }}</td>
                    <td>{{ $n->author }}</td>
                    <td>{{ $n->message }}</td>
                    <td>{{ $n->active }}</td>
                    <td>{{ $n->picture_path }}</td>
                    <td>{{ Form::checkbox('active') }}</td>
                    <td>{{ link_to_route('admin.destroy', 'Delete', array($n->id)) }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>

当您想要在用户单击没有 ajax 的复选框时更新新闻项时,这是一种方法:

路由器.php

Route::post('admin/{id}/active',
    [
        'uses' => 'NyhetsController@toggleActive',
        'as' => 'admin.toggle_active'
    ]
);

HTML index.blade.php

@if($news->count())
    <table class="table table-striped table-boarded">
        <thead>
            <tr>
                <th>ID</th>
                <th>Title</th>
                <th>Author</th>
                <th>Message</th>
                <th>Picture path</th>
                <th>Active</th>
            </tr>
        </thead>
        <tbody>
            @foreach($news as $n)
                <tr>
                    <td>{{ $n->id }}</td>
                    <td>{{ $n->title }}</td>
                    <td>{{ $n->author }}</td>
                    <td>{{ $n->picture_path }}</td>                        
                    <td>{{ $n->message }}</td>
                    <td>
                        {{ Form::open(array('url' => 'admin/' . $n->id . '/active')) }}
                        {{ Form::submit('', [ 'class' => ($n->active ? 'glyphicon glyphicon-ok' : 'glyphicon glyphicon-remove') ]);
                        {{ Form::close() }}
                    <td>{{ link_to_route('admin.destroy', 'Delete', array($n->id)) }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>
@endif

NyhetsController

public function toggleActive($id) {
    $news = News::findOrFail($id);
    if($news->active)
    {
        $news->active = 'false';
        $news->update($input);
    }
    else{
        $news->active = 'true';
        $news->update($input);
    }
}
return Redirect::to('admin');

代码未经测试!

在您的表单中:

{{ Form::checkbox('active', 'true') }}

在控制器中

$news = News::findOrFail($id);
if (Input::get('active') === 'true') {
    $news->active = 'true';  // UPDATE:must be true of course
    $news->update($input);
} else {
    $news->active = 'false'; // UPDATE:must be false of course
    $news->update($input);
}

顺便说一句:如果您只是使用

Route::resource('admin', 'NyhetsController');