laravel控制器更新无法正常工作

时间:2017-02-21 03:07:43

标签: php laravel-5.1

我正在使用RESTful资源控制器并且更新功能无法正常工作。

如果生成这样的链接并且没有任何反应 http://localhost:8000/medication/3?_token=Sv0mblJUcWppsO6roWiXOzZzcFOqlgMBIzJoy3HW&medicine=a&dosage=a&howOften=aaa&forWhat=a&doctor=a&vaccination=a&shots=a

我的视图代码(仅用于测试)是                           

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                </button>
                <h4 class="modal-title custom_align" id="Heading">Edit Your Medication</h4>
            </div>

            <form role="form" action="/medication/3" method="PUT" class="f1">
            {{ csrf_field() }}
            <div class="modal-body">

                <div class="form-group">
                    <input type="text" name="medicine" class="form-control" placeholder="Medicine">
                </div>
                <div class="form-group">
                    <input type="text" name="dosage" class="form-control" placeholder="Dosage">
                </div>
                <div class="form-group">
                    <input type="text" name="howOften" class="form-control" placeholder="How often do I take it">
                </div>
                <div class="form-group">
                    <input type="text" name="forWhat" class="form-control" placeholder="what it is for?">
                </div>
                <div class="form-group">
                    <input type="text" name="doctor" class="form-control" placeholder="Prescribing Doctor">
                </div>
                <div class="form-group">
                    <input type="text" name="vaccination" class="form-control" placeholder="Vaccination">
                </div>
                <div class="form-group">
                    <input type="text" name="shots" class="form-control" placeholder="Shots">
                </div>
            </div>

            <div class="modal-footer ">
                <button type="submit" class="btn btn-success btn-lg btn-update" style="width: 100%;"><span class="glyphicon glyphicon-ok-sign"></span> Update</button>
            </div>
            </form>

        </div><!-- /.modal-content --> 
    </div><!-- /.modal-dialog -->

</div>

控制器代码

public function update(Request $request, $id)
{
    $row = $id;

    $data = array('medicine' => $request->medicine,
                    'dosage' => $request->dosage,
                    'howOften' => $request->howOften,
                    'forWhat' => $request->forWhat,
                    'doctor' => $request->doctor,
                    'vaccination' => $request->vaccination,
                    'shots' => $request->shots);

    $ex = ProfileInformation::insertInformation($row, $this->user, $this->TABLE_NAME, $data);
    if($ex){//executed properly
        return redirect('/medication');
    } else {
        return redirect('/customer');
    }

}

我没有使用laravel的标准id主键但仍想坚持使用资源控制器。我不明白为什么一切都没发生。如果有查询错误,那么它至少应该转到我的重定向代码。

1 个答案:

答案 0 :(得分:1)

由于您设置了method="PUT",因此无效。而是执行以下操作

<form role="form" action="/medication/3" method="POST" class="f1">
        {{ csrf_field() }}
        {{ method_field("PUT") }}