erikms61's avatar

eloquent update doesn't seem to happen

Hi all, I just ran into a really weird problem: not all Eloquent model updates seem to happen. So far, I have this happen on only one model, but I can't see what I am doing wrong. Some tinker code below.

EDIT: this is laravel 10.10

First: I update the min_value field using update.

No problem there: it is set to the value I provide

> $sc->update(['min_value'=>17])
= true
> $sc->refresh()
= App\Models\SensorChannel {#6831
    id: "99d4fd6d-5851-43d2-a9b5-062bccd02243",
    min_value: 17,
    max_value: 14,
    crit_value: 93,
    sensor_id: "99d4fd6d-53b9-4791-b1a7-a28783c0347a",
    sensor_type_id: 5,
    created_at: "2023-08-07 09:30:39",
    updated_at: "2023-08-07 09:31:45",
  }

Then the max_value...

Hm this is weird. mysql confirms that no update has happened

> $sc->update(['max_value'=>15])
= true
> $sc->refresh()
= App\Models\SensorChannel {#6831
    id: "99d4fd6d-5851-43d2-a9b5-062bccd02243",
    min_value: 19,
    max_value: 14,
    crit_value: 93,
    sensor_id: "99d4fd6d-53b9-4791-b1a7-a28783c0347a",
    sensor_type_id: 5,
    created_at: "2023-08-07 09:30:39",
    updated_at: "2023-08-07 09:32:01",
  }

However, if I set the field, then save, the update does happen

> $sc->max_value=10
= 10
> $sc->save()
= true
> $sc->refresh()
= App\Models\SensorChannel {#6831
    id: "99d4fd6d-5851-43d2-a9b5-062bccd02243",
    min_value: 19,
    max_value: 10,
    crit_value: 93,
    sensor_id: "99d4fd6d-53b9-4791-b1a7-a28783c0347a",
    sensor_type_id: 5,
    created_at: "2023-08-07 09:30:39",
    updated_at: "2023-08-07 09:39:07",
  }

Any ideas? please?

0 likes
8 replies
D. Eikelboom's avatar

Is your max_value in your $fillables array on your model?

erikms61's avatar
erikms61
OP
Best Answer
Level 1

@D. Eikelboom thank you for your swift response. Yes it is: this is the $fillables on the SensorChannel model :

    protected $fillable = [
        'crit_value'.
        'max_value',
        'min_value',
        'sensor_id',
        'sensor_type_id',
    ];

This is really too dumb. My apologies for wasting your time and energy on this. The error is mine entirely: ZOOM IN: 'crit_value'<dot> instead of <comma>. The exact effect of this was that I'd created a field 'crit_valuemax_value' which of course never got filled.

The tinker values come from the database, which gives the correct field names. Thank the gods of step-debugging for their hard work.

Snapey's avatar

Since you are using non-integer primary key, have you set

$protected $incrementing=false;

on your model?

erikms61's avatar

@Snapey according to tinker $incrementing is supposed to be public, but I tried it out, and I don't see any change: and it's not just the max_value, only the min_value is getting updated. Could this be a matter of reserved words?

> $sc->update(['max_value'=>11,'crit_value'=>20,'min_value'=>18])
= true
> $sc->refresh()
= App\Models\SensorChannel {#7491
    id: "99d4fd6d-5851-43d2-a9b5-062bccd02243",
    min_value: 18,
    max_value: 10,
    crit_value: 93,
    sensor_id: "99d4fd6d-53b9-4791-b1a7-a28783c0347a",
    sensor_type_id: 5,
    created_at: "2023-08-07 09:30:39",
    updated_at: "2023-08-07 10:21:55",
  }

Snapey's avatar

@erikms61 How about if you try;

$sc->max_value = 11;
$sc->crit_value = 20;
$sc->save();
erikms61's avatar

@Snapey oh that will work, but it won't play nice with Filament (or at least I'll have to write out the update logic, which I was trying to avoid):

> $sc->max_value=11
= 11
> $sc->crit_value=20
= 20
> $sc->save()
= true
> $sc->refresh()
= App\Models\SensorChannel {#7491
    id: "99d4fd6d-5851-43d2-a9b5-062bccd02243",
    min_value: 18,
    max_value: 11,
    crit_value: 20,
    sensor_id: "99d4fd6d-53b9-4791-b1a7-a28783c0347a",
    sensor_type_id: 5,
    created_at: "2023-08-07 09:30:39",
    updated_at: "2023-08-07 10:23:30",
  }
Snapey's avatar

@erikms61 The idea was to eliminate it being a mass assignment issue, but looks like you got there anyway 😀

Please or to participate in this conversation.