Update with Eloquent Hi guys,
I'm updating a record, but at the end I could not get. This is my code
$device = Devices::find($id);
$device->deleted = 1;
$device->save();
The method "find" works , because after i send values as parameters in sight.
Thx for help.
What is your question here?
Updating models can be done like your way and by doing something like this
$device = Devices::find($id);
$device->update([
'deleted' => 1
]);
Now if find returns null you obviously can't update the model. Laravel has some protection for that as well
$device = Device::findOrFail($id);
my question is, why not check updated ? and probe the two options and still do not, that more should be considered?
¯(°_o)/¯ In the nicest way I can't work out exactly what you are getting at, but to protect against mass-assignment might be the answer to that if I had to guess.
@phildawson I have no clue either... That might explain the different answers ;)
guys!!
finally it worked this way , since in the where the update came out "id = null " . What am I doing wrong?
Devices::where('ID', '=', $id)->update(array('deleted' => 1));
What am I doing wrong?
have you changed what $id is from the last time you tested? :) that code should be equivalent of this. Does this work?
Devices::find($id)->update(['deleted' => 1]);
When in doubt temporally put in an integer of a device you know exists.
Devices::find(1)->update(['deleted' => 1]); // update device 1
2 years later, I've fixed this by using ur code.
Devices::where('ID', '=', $id)->update(array('deleted' => 1));
None of the suggestions from the others doesnt work on my code. My question is "are you using Lumen by any chance?" .
What was the laravel version on the time this thread was posted?
@YOS_GANI - Seems perhaps you need to add protected $primaryKey = 'ID'; to your class?
Please sign in or create an account to participate in this conversation.