Reputation: 1280
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
set0
= $field whereid
= 1)
Upvotes: 1
Views: 2020
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);
Upvotes: 0
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