Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

miguelcamargo9's avatar

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.

0 likes
10 replies
bobbybouwmann's avatar

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); 
4 likes
miguelcamargo9's avatar

my question is, why not check updated ? and probe the two options and still do not, that more should be considered?

phildawson's avatar

¯(°_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.

miguelcamargo9's avatar

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));
phildawson's avatar
Level 26

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 likes
yos_gani's avatar

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?

jrebs's avatar

@YOS_GANI - Seems perhaps you need to add protected $primaryKey = 'ID'; to your class?

Please or to participate in this conversation.