user9888683
user9888683

Reputation:

QueryException error while updating data

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>

Upvotes: 0

Views: 89

Answers (2)

Pusparaj
Pusparaj

Reputation: 1639

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

Upvotes: 0

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

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)

Upvotes: 2

Related Questions