Laravel:检查复选框是否为真或假,然后更新数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28299999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:49:19  来源:igfitidea点击:

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

phplaravel

提问by Smajo

Just started with laravel using phpStorm. Im trying to create a form that take inputs, and a checkbox, then adds it to a database. All of the rows in the database get listed in the view index.html. The user should be allowed to activate or deactivate (with the checkbox) the the message that should be listed when browsing the index.html file. Because if a news article is active, and you want to deactivate it, you should be able to just uncheck the checkbox, and it will be deactivated. So I am wondering how my function should look like.

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

Im also unsure how to run the function ActivatedOrDeactivated when the checkbox gets checked or unchecked.

当复选框被选中或取消选中时,我也不确定如何运行 ActivatedOrDeactivated 函数。

Its my first post, so please tell if something is hard to understand in my description.

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

My route:

我的路线:

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'
    ]
);

My Controller that contains the function (just something i wrote in anger):

我的控制器包含该功能(只是我生气写的东西):

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

My index file that contains the form:

我的包含表单的索引文件:

<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>

回答by blablabla

When you want to update the news item when a user clicks the checkbox without ajax this would a way to do it:

当您想在用户单击没有 ajax 的复选框时更新新闻项目时,可以这样做:

router.php

路由器.php

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

HTML index.blade.php

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

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');

Code is untested!

代码未经测试!

回答by nozzleman

In your Form::

在您的表格中::

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

In your Controller

在您的控制器中

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

By the way: you could save yourself some typing if you simply route restfullyusing

顺便说一句:你可以,如果你只是救自己一些打字路线REST的使用

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