Geoff
Geoff

Reputation: 6649

Laravel update creates a new record

AM trying to update existing record with eloquent via

 return Users::where('id', $id)
        ->update(['names' => $request->input("name")]);

But the above creates a new record. What am i missing on this? I would like this to be one line as above.

Upvotes: 0

Views: 689

Answers (1)

rakeysharyal
rakeysharyal

Reputation: 96

You have missed many things, such as coding convention of Laravel. Indeed, we usually create model in singular firm but you created "Users". Instead of "Users" you can create "User" model. And the syntax is also incorrect. So you can follow the below code. It should work if you have created table, model and controller in a correct way.

return User::where('id', $id)->first()->update(['name' => $request->input("name")]);

Or

return User::findOrFail($id)->update(['name' => $request->input("name")]);

Upvotes: 2

Related Questions