0

Into database not update data, i can't understand my problem, error is below

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)

This is my controller

 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));

        }
    }

}

This is my edit.blade.php file

{!! 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() !!}

This is my create.blade.php file

<td><a href="{{route('member.add-single-trade.edit', $trade->id)}}">{{$trade->stoploss}}</a></td>
3
  • you can simply add the _method field to the db or investigate where that field is required. In your code there is no mention for it Commented Jul 21, 2018 at 13:11
  • ok i am checking -@DaFois Commented Jul 21, 2018 at 13:12
  • Post your model code Commented Jul 21, 2018 at 13:22

2 Answers 2

2

You haven't included your model code (probably you are using $guarded = [];) but probably instead of

$input = $request->all();

you should use:

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

This is because additional _method field is added to form to pass HTTP verb method when using Form::model and obviously you don't have _method field in your table

EDIT

In case you have more fields you can include more fields to ignore for example:

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

or you can only use get fields you really want to get for example:

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

(Of course you should add more fields to above - this is only example)

Sign up to request clarification or add additional context in comments.

5 Comments

error is changed 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 Please take a look at my updated answer
again error: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'reason' in 'field list' (SQL: update trades` set exchange_id = 1, market_id = 1, symbol_id = 45, is_action = 0, tradedate = , rate = 600, reason = 3, note = hhhhhhhhh, updated_at = 2018-07-21 13:26:44 where trades.user_id = 33 and trades.user_id is not null and trades.deleted_at is null) ◀"`
@KinnariPrajapati Please read carefully my answer. You should exclude (using except) or include only some fields (using `only) more fields than I showed. You know your table structure, so fill it in more values after comma and it will work
0

You can use $fillable property of the model to tell ORM which attributes (columns) can be mass assignable. Any other fields passed via update and create methods will be discarded. check at Laravel docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.