【问题标题】:How to update table column using checkbox?如何使用复选框更新表格列?
【发布时间】:2017-01-02 04:52:28
【问题描述】:

当复选框被选中时,我正在尝试将我的列 isApprove 更新为 true 或 1。正如你在我看来。我在我的复选框中添加了一个 href 以查找所选复选框的 id。但不幸的是在检查复选框时给了我一个错误。我错过了什么吗?任何帮助将不胜感激!

缺少 [Route: document.pending] [URI: documents/pending/approve/{id}] 的必需参数。 (查看:C:\Users\JohnFrancis\LaravelFrancis\resources\views\document\create.blade.php)

查看

待定.blade.php

<tbody>
        @foreach ($pendingDocumentLists as $list)
        <tr class = "info">

        <td>{{ $list->title }}</td>

        <td>

        @if (Auth::id() == $list->approver_id)

        <form class = "form-inline" method = "post" action = "{{ route ('document.pending', $list->id) }}">


                <div class = "form-group">
                    <a href = "{{ route ('document.pending', $list->id) }}">
                        <input type = "checkbox" name = "approve" value = "yes">
                            <strong>Approve</strong>
                        </input>
                    </a>
                </div>

                <div class = "form-group">

                    <a href = "{{ route ('document.pending', $list->id) }}">
                    <input type = "checkbox" name = "reject">
                        <strong>Reject</strong>
                    </input>
                   </a>
                </div>

        </form>
            @endif
            </td>   
        </tr>
    @endforeach
</tbody>

控制器

public function updateIsApprove($id)
{
    $document = Document::find($id);

    if($approve == "yes")
    {
        $document->isApprove = 1;
        $document->save();
    }
}

路线

Route::get('/documents/pending',
[
   'uses' => '\App\Http\Controllers\DocumentController@documentsSentForApproval',
   'as' => 'document.pending',
]);

Route::post('/documents/pending/approve/{id}',
[
  'uses' => '\App\Http\Controllers\DocumentController@updateIsApprove',
  'as' => 'document.pending',
]);

【问题讨论】:

  • 所包含的错误与您的问题无关。错误是说路由/url中没有提供{id}。
  • @RyanVincent 我只是在选中复选框时尝试设置为 true。如果我将复选框值作为我的 id 传递,它是否有效?谢谢

标签: php laravel laravel-5.2 laravel-routing


【解决方案1】:

当您没有检查输入时 - 没有发送抛出路由器。并且在您的路由器中需要 {id} 属性 您可以在复选框之前添加具有相同名称的隐藏输入。

 <input type = "hidden" name = "approve" value = "no">

如果复选框被选中,它会重写隐藏的输入值。如果未选中复选框,则使用相同的名称,但使用另一个值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多