JIJOMON K.A
JIJOMON K.A

Reputation: 1280

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list'

I write a lumen code to update my table fields, this is my code -

$field='';
if($request->input('category')){
   $val=$request->input('category');
   $field="'category' => '$val'";
}
if($request->input('subcategory')){
   $val=$request->input('subcategory');
   if($field==''){
      $field="'sub_category' => '$val'";
   } else{
      $field.=",'sub_category' => '$val'";
   }
}
// return $field;

$Expence=DB::table('expencedetails')
           ->where('id',$request->input('id'))
           ->update(['$field']);

the value of $field is 'category' => 'fgfg', 'sub_category' => 'ggg'.

when I run the code it shows an error

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: update expencedetails set 0 = $field where id = 1)

Upvotes: 1

Views: 2020

Answers (2)

M Khalid Junaid
M Khalid Junaid

Reputation: 64486

You might need to rewrite your logic as

$fields=array();
if($request->input('category')){
    $fields['category'] = $request->input('category');
}
if($request->input('subcategory')){
    $fields['sub_category'] = $request->input('subcategory');
}
$Expence=DB::table('expencedetails')
           ->where('id',$request->input('id'))
           ->update($fields);
  • If request has category then update category
  • If request has only sub_category then update sub_category
  • If request has both category and sub_category then update both

Upvotes: 0

Ravinder Reddy
Ravinder Reddy

Reputation: 24012

Refer to https://laravel.com/docs/5.0/queries#updates

Syntax:

->update(['votes' => 1]);

I think your update has missing parameter

->update(['$field'])

Following change may help:

->update(['your_column_name_here' => $field])

Upvotes: 1

Related Questions