Reputation: 73
I working on laravel project and trying to update record using query builder but it not working , this is the function where I trying to edit the record in it
public function editchecksave(Request $request)
{
$id = $request->id;
$username = $request->username;
$attribute = $request->attribute;
$value = $request->value;
$op = $request->op;
$result = DB::update('update radcheck set username = "?" ,
attribute="?",value="?" ,op="?" where id = ?',
[$username,$attribute,$value,$op,$id]
);
if ($result)
{
return response()->json([ 'msg' => 'Updated Successfully' . $username]);
}
return response()->json([ 'msg' => 'Not Updated Successfully'. $value]);
}
this is the route
Route::get( 'radcheckedit', 'radcheckController@editchecksave' );
when I do not change the first element in the update query it not updated but when I change the first element it updated successfully .in other words (when I change the value of username it updated successfully and when I do not change the username not updated , when I put the attribute first element in the query it must change the value of it to make update so user must change the first value of query and it not good because user may be doesn't want to edit this element)
it give me the Message Not updated successfully
Upvotes: 0
Views: 18708
Reputation: 33186
You shouldn't put a raw query in the update
function, it requires an array of to-update values. You can write your query using the build-in querybuilder:
$result = DB::table('radcheck')
->where('id', $id)
->update([
'username' => $username,
'attribute' => $attribute,
'value' => $value,
'op' => $op
]);
Or even better without having to fetch the variables from the request manually:
$result = DB::table('radcheck')
->where('id', $id)
->update($request->only(['username', 'attribute', 'value', 'op']));
This will also make the code more readable.
Upvotes: 7