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.
Your table shows id
is the PK, your model has date
as the PK. They should be the same.
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!!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community