Support the ongoing development of Laravel.io →
posted 7 years ago
Eloquent
Last updated 2 years ago.
0

What does your update code, model, and db table look like?

0

Model:

class MonthlyUpdateStatus extends Model
{
protected $table = 'monthly_update_status';
protected $primaryKey = 'date';
protected $guarded = [];	
}

DB Table:

+------------------+------------------+------+-----+---------+----------------+
| Field            | Type             | Null | Key | Default | Extra          |
+------------------+------------------+------+-----+---------+----------------+
| date             | varchar(255)     | NO   |     | NULL    |                |
| step             | int(11)          | NO   |     | NULL    |                |
| created_at       | timestamp        | YES  |     | NULL    |                |
| updated_at       | timestamp        | YES  |     | NULL    |                |
| type             | int(11)          | NO   |     | NULL    |                |
| id               | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
+------------------+------------------+------+-----+---------+----------------+

Code:

$updateStatus = MonthlyUpdateStatus::where(['date' => $dateNow, 'type' => 1])->first();
$updateStatus->step = $nextStep;
$updateStatus->save();

Result: If I have in the table more than one record for this date (e.g. with type=1 and type=2), all will be updated with step=$nextStep.

Last updated 7 years ago.
0

Your table shows id is the PK, your model has date as the PK. They should be the same.

0

Yes, that was the problem. I had made the id to be the PK in the Model and it has started working correctly. Many thanks for the help!!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Panda Gm Panda Gm pandagm Joined 26 Jul 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.