【问题标题】:Laravel: Check checkbox if true or false, then update databaseLaravel:检查复选框是否为真或假,然后更新数据库
【发布时间】:2015-02-03 13:22:21
【问题描述】:

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

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

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

我的路线:

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>

【问题讨论】:

  • 复选框在未选中时不会被发布(它不在 $_POST 数组中)。你可以通过给 active 一个默认值来解决这个问题。输入::get('active', false);
  • 不,因为我不太明白当复选框被选中或取消选中时如何运行该函数
  • 当用户选中表格中的复选框时是否要保存该行?你的复选框周围没有表单标签,所以我想你想用 javascript/jquery 来做?
  • 我对这一切都很陌生,所以起初我试图在没有 javascript 和 jquery 的情况下完成这一切。如果用户选中或取消选中复选框,我只想更新数据库中的值。这样我以后可以在另一个视图中只显示激活的新闻。

标签: php laravel


【解决方案1】:

当您想在用户单击不带 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

Nyhets 控制器

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

代码未经测试!

【讨论】:

  • 我还建议不要提供删除新闻项目的链接,而是使用 REST 方法。您必须将删除包装在一个表单中,以便您可以使用 DELETE http 方法发布它。 code.tutsplus.com/tutorials/…
  • 成功了,只是需要稍微调整一下!感谢您的帮助!
【解决方案2】:

在您的表单中::

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

,你可以节省一些打字时间
Route::resource('admin', 'NyhetsController');

【讨论】:

  • 它实际上在大多数情况下都有效。如果索引中显示的行处于活动状态 (true),并且您单击复选框,则数据库中的值将更新为非活动 (false)。但是当我尝试激活一个不活跃的帖子时,什么也没有发生。也没有错误。那么是不是意味着Controller中的逻辑不正确呢?
  • 好吧,我想我换了它:/。生病更新答案。愚蠢的错字......对不起'回合......首先在问题中是错误的;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-02
  • 1970-01-01
  • 2016-10-19
  • 2012-08-03
  • 1970-01-01
  • 2014-05-10
相关资源
最近更新 更多