如何更新数据库中的内容?

时间:2019-09-24 12:07:37

标签: laravel

我正在尝试更新从数据库中获得的值,但似乎并没有使用路由功能

我尝试更改route方法,甚至拒绝了请求,但似乎不起作用

这是路线

Route::put("/users/bonus/update/{id}", 
       [
         "uses" => "AdminDashboardController@updatebonus",
         "as"=> "userbonus.mer"
       ]);

下面是被调用以更新的函数

public function updatebonus(Request $request, $id)
{

    if (auth()->user()->isAdmin != 1) {
        return redirect()->route('home');
    } else if (auth()->user()->isAdmin == 1) {

        $bonus=OtherBonus::where('id','=','$id')->first();
        $bonus->card_bonus=trim(strip_tags($request['cbonus']));
        $bonus->monthly_bonus=trim(strip_tags($request['mbonus']));
        $bonus->travelling_bonus=trim(strip_tags($request['tbonus']));
        $bonus->festival_bonus=trim(strip_tags($request['fbonus']));
        $bonus->save();

        return redirect()->back()->with("success", "Bonus Settings 
Successfully updated");
    }
}

下面是将数据传递给与数据库通信的函数的形式。

    <form class="form-horizontal form-label-left"  action="{{route('userbonus.mer', ['id'=>$bonus['id']])}}" method="PUT">

                            <span class="section">General Bonus in %age</span>

                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="cbonus">Card
                                    Bonus <span class="required">*</span>
                                </label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <input class="form-control col-md-7 col-xs-12"
                                           value="{{$bonus['card_bonus']}}" name="cbonus"
                                           placeholder="10.00%" required="required" type="text">
                                </div>
                            </div>

                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="fbonus">Festival
                                    Bonus<span class="required">*</span>
                                </label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <input type="text" name="fbonus" required="required"
                                           value="{{$bonus['festival_bonus']}}"
                                           class="form-control col-md-7 col-xs-12" placeholder="34.6%">
                                </div>
                            </div>

                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="mbons">Monthly
                                    Bonus <span class="required">*</span>
                                </label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <input type="text" id="email" name="mbonus" required="required"
                                           value="{{$bonus['monthly_bonus']}}" placeholder="56.9%"
                                           class="form-control col-md-7 col-xs-12">
                                </div>
                            </div>

                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="tbonus">Travelling
                                    Bonus <span class="required">*</span>
                                </label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <input type="text" id="email" name="tbonus" placeholder="0.89%"
                                           value="{{$bonus['travelling_bonus']}}" required="required"
                                           class="form-control col-md-7 col-xs-12">
                                </div>
                            </div>



                            <div class="form-group">
                                <div class="col-md-6 col-md-offset-3">
                                    <button type="reset" class="btn btn-primary">Reset</button>
                                    <button type="submit" class="btn btn-success">Submit</button>
                                </div>
                            </div>
                        </form>

2 个答案:

答案 0 :(得分:0)

尝试一下

public function updatebonus(Request $request, $id)
{

    if (auth()->user()->isAdmin != 1) {
        return redirect()->route('home');
    } else if (auth()->user()->isAdmin == 1) {

        $bonus=OtherBonus::find($id);
        $bonus->update([
          'monthly_bonus'=>$request->mbonus,
           'travelling_bonus'=>$request->tbonus,
          'festival_bonus'->$request->fbonus;
]);


        return redirect()->back()->with("success", "Bonus Settings 
Successfully updated");
    }
}

在发布之后添加刀片文件,然后添加{{method_field('PUT')}}                                 {{csrf_field()}}

   <form class="form-horizontal form-label-left"  action="{{route('userbonus.mer', ['id'=>$bonus['id']])}}" method="post">
                             {{method_field('PUT')}}
                                {{csrf_field()}}

                            <span class="section">General Bonus in %age</span>

                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="cbonus">Card
                                    Bonus <span class="required">*</span>
                                </label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <input class="form-control col-md-7 col-xs-12"
                                           value="{{$bonus['card_bonus']}}" name="cbonus"
                                           placeholder="10.00%" required="required" type="text">
                                </div>
                            </div>

                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="fbonus">Festival
                                    Bonus<span class="required">*</span>
                                </label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <input type="text" name="fbonus" required="required"
                                           value="{{$bonus['festival_bonus']}}"
                                           class="form-control col-md-7 col-xs-12" placeholder="34.6%">
                                </div>
                            </div>

                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="mbons">Monthly
                                    Bonus <span class="required">*</span>
                                </label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <input type="text" id="email" name="mbonus" required="required"
                                           value="{{$bonus['monthly_bonus']}}" placeholder="56.9%"
                                           class="form-control col-md-7 col-xs-12">
                                </div>
                            </div>

                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="tbonus">Travelling
                                    Bonus <span class="required">*</span>
                                </label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <input type="text" id="email" name="tbonus" placeholder="0.89%"
                                           value="{{$bonus['travelling_bonus']}}" required="required"
                                           class="form-control col-md-7 col-xs-12">
                                </div>
                            </div>



                            <div class="form-group">
                                <div class="col-md-6 col-md-offset-3">
                                    <button type="reset" class="btn btn-primary">Reset</button>
                                    <button type="submit" class="btn btn-success">Submit</button>
                                </div>
                            </div>
                        </form>

在路线上发布

答案 1 :(得分:0)

HTML表单不接受PUT作为方法。它仅适用于GETPOST。看看here。所以如果你写

<form method="PUT"></form>

您将作为GET请求提交表单。 Laravel侦听表单提交后必须呈现的特定输入,以便识别PUT方法。看看official documentation

  

HTML表单不支持PUT,PATCH或DELETE操作。因此,在定义从HTML表单调用的PUT,PATCH或DELETE路由时,您将需要向表单添加一个隐藏的_method字段。通过_method字段发送的值将用作HTTP请求方法:

<form action="/foo/bar" method="POST">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

您可以使用@method Blade指令生成_method输入:

<form action="/foo/bar" method="POST">
    @method('PUT')
    @csrf
</form>