【问题标题】:QueryException error while updating data更新数据时出现 QueryException 错误
【发布时间】:2018-12-29 14:01:24
【问题描述】:

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

照亮\数据库\查询异常(42S22) SQLSTATE[42S22]:未找到列:1054 '字段列表'中的未知列'_method'(SQL:更新trades 设置_method = PATCH,_token = AcbGEbEyNxX3e2RzRR2cb1SW6NDvkJuDqFevl0Mr,exchange_id = 1 1, symbol_id = 45, is_action = 0, tradedate = , rate = 5000, note = hhhhhhhhh, updated_at = 2018-07-21 13:0332:13 其中@9876. user_id = 33 和 trades.user_id 不为空,trades.deleted_at 为空)

这是我的控制器

 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>

【问题讨论】:

  • 您可以简单地将_method 字段添加到数据库中,或者调查需要该字段的位置。在您的代码中没有提及它
  • 好的,我正在检查 -@DaFois
  • 发布您的型号代码

标签: laravel laravel-5 laravel-query-builder


【解决方案1】:

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

$input = $request->all();

你应该使用:

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

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

编辑

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

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

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

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

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

【讨论】:

  • 错误已更改1054 Unknown column '_token' in 'field list'
  • error is :` "SQLSTATE[42S22]: Column not found: 1054 Unknown column '_token' in 'field list' (SQL: update trades set _token = AcbGEbEyNxX3e2RzRR2cb1SW6NDvkJuDqFevl0Mr, ``
  • @KinnariPrajapati 请看看我更新的答案
  • 再次错误:"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'reason' in 'field list' (SQL: update trades` 设置exchange_id = 1, market_id = 1, symbol_id = 45, is_action = 0, tradedate = , rate = 600,reason = 3,note = hhhhhhhhhh,updated_at = 2018-07-21 13:26:44 其中trades.user_id = 33 和trades.user_id 不为空trades.deleted_at 为空)◀"`
  • @KinnariPrajapati 请仔细阅读我的回答。您应该排除(使用except)或仅包含一些字段(使用`only)比我显示的更多的字段。你知道你的表结构,所以在逗号后面填写更多的值,它会起作用
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2021-12-11
    • 1970-01-01
    • 2021-07-01
    • 2016-12-21
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多