更新数据时出现 QueryException 错误

QueryException error while updating data

进入数据库不更新数据,我无法理解我的问题,错误如下

Illuminate \ Database \ QueryException (42S22) SQLSTATE[42S22]: Column not found: 1054 Unknown column '_method' in 'field list' (SQL: update trades set _method = PATCH, _token = AcbGEbEyNxX3e2RzRR2cb1SW6NDvkJuDqFevl0Mr, exchange_id = 1, market_id = 1, symbol_id = 45, is_action = 0, tradedate = , rate = 5000, note = hhhhhhhhh, updated_at = 2018-07-21 13:06:13 where trades.user_id = 33 and trades.user_id is not null and trades.deleted_at is null)

这是我的控制器

 public function edit($id)
{

    $trade = Trade::findOrFail($id);

    $exchanges = Exchange::pluck('exchange','id')->all();
    $markets = Market::pluck('market','id')->all();
    $symbols = Symbol::pluck('symbol','id')->all();
    $reasons = Reason::pluck('reason','id')->all();

    return view('member.add-single-trade.edit', compact('trade','reasons', 'exchanges', 'markets', 'symbols'));
}


public function update(Request $request, $id)
{        
    $input = $request->all();
    $tradeID= Auth::user()->trade($id)->update($input);
    $reasons=$request->input('reason');

    $data = [];
    foreach($reasons as $key => $value) {

        $data[] = ['reason_id' => $value];

    };
                                  $data = array(

                                    'reason_id' =>$reasons,
                                    'trade_id' => $tradeID->id,
                                  );


                                         $test['trade_id']= $tradeID->id;

    if($data > 0) {

        foreach ($data as $datum) {


            $tradeID->tradereason()->update(new TradeReason($datum));

        }
    }

}

这是我的 edit.blade.php 文件

{!! Form::model($trade,['method'=>'PATCH', 'action'=> ['trades\AddSingleTradeController@update',$trade->id]]) !!}


<div class="col-sm-10">

    <div class="form-group col-sm-5">
        {!! Form::label('exchange_id', 'Exchanges:') !!}
        {!! Form::select('exchange_id', [''=>'Choose Options'] + $exchanges , null, ['class'=>'form-control'])!!}
    </div>

    <div class="form-group col-sm-5">
        {!! Form::label('market_id', 'Markets:') !!}
        {!! Form::select('market_id', [''=>'Choose Options'] + $markets, null, ['class'=>'form-control'])!!}
    </div>

    <div class="form-group col-sm-10">
        {!! Form::label('symbol_id', 'Symbols:') !!}
        {!! Form::select('symbol_id', [''=>'Choose Options']+ $symbals   , null, ['class'=>'form-control'])!!}
    </div>

    <div class="form-group col-sm-10">

        {{ Form::radio('is_action', 1) }} Buy
        {{ Form::radio('is_action', 0) }} Sell
    </div>

    <div class="form-group col-lg-5">
        {!! Form::label('tradedate', 'Traded date:') !!}
        {!! Form::date('tradedate', null, ['class'=>'form-control'])!!}
    </div>

    <div class="form-group col-lg-5">
        {!! Form::label('rate', 'Traded Rate:') !!}
        {!! Form::text('rate', null, ['class'=>'form-control'])!!}
    </div>


    <div class="form-group col-sm-10">
        {!! Form::label('reason', 'Choose Reasons:') !!}
        {{Form::select('reason',$reasons,null, array('id'=>'reasons','multiple'=>'multiple','name'=>'reason[]',"class"=>"js-example-basic-multiple form-control", 'data-width'=>'60%', 'data-live-search'=>'true','onchange' => 'all_function()'))}}

    </div>


    <div class="form-group col-lg-10">
        {!! Form::label('note', 'Note:') !!}
        {!! Form::textarea('note', null, ['class'=>'form-control', 'rows' => 2, 'cols' => 40])!!}
    </div>



    <div class="form-group col-lg-4">
        {!! Form::submit('Save', ['class'=>'btn btn-success btn-lg']) !!}
    </div>



    {!! Form::close() !!}




    <div class="form-group col-lg-4">
    {!! Form::open(['method'=>'DELETE', 'action'=> ['trades\AddSingleTradeController@destroy', $trade->id]]) !!}

    <div class="form-group">
        {!! Form::submit('Delete', ['class'=>'btn btn-danger btn-lg']) !!}
    </div>

    </div>

    {!! Form::close() !!}

这是我的 create.blade.php 文件

<td><a href="{{route('member.add-single-trade.edit', $trade->id)}}">{{$trade->stoploss}}</a></td>

您没有包含模型代码(可能您使用的是 $guarded = [];),但可能不是

$input = $request->all();

你应该使用:

$input = $request->except('_method');

这是因为在使用 Form::model 时向表单添加了额外的 _method 字段以传递 HTTP 动词方法,显然您的 [=34= 中没有 _method 字段]

编辑

如果您有更多字段,您可以包含更多要忽略的字段,例如:

$input = $request->except('_method', '_token');

或者您只能使用您真正想要获取的字段,例如:

$input = $request->only('exchange_id', 'market_id');

(当然你应该在上面添加更多的字段 - 这只是例子)

您可以使用模型的 $fillable 属性 来告诉 ORM 哪些属性(列)可以被批量分配。通过更新和创建方法传递的任何其他字段都将被丢弃。 check at Laravel docs