Updating record with custom action

i want to update data from another model with custom action, but the result always false(not updated), So far, this is what I've done. Function save is running smoothly btw
No description
Solution:
```php Schedule::updateOrCreate( ['ticket_header_ticket_id' => $ticket->ticket_id], ['schedule' => $data['schedule']] )...
Jump to solution
7 Replies
EL REKT
EL REKT5mo ago
any suggestion guys ? what did i do wrong ?
Saade
Saade5mo ago
$schedule->update() will do nothing if you dont pass the attributes you want to update as an argument. Dont you mean ->save()?
EL REKT
EL REKT5mo ago
previously I had an update function that ran smoothly.. like this.. I think the way I add attributes is the same, but it doesn't work in other actions, or is there another way to add attributes for updates?
No description
Saade
Saade5mo ago
there's no difference between ->save() and ->update() on your implementation, only the fact that its not working because you're not passing anything to ->update();
$row = ...

if(is_null($row)) {
return;
}

$schedule->schedule = ....
$schedule->ticket_header_ticket_id = ...

$schedule->save();

Notification::make('Updated')...
$row = ...

if(is_null($row)) {
return;
}

$schedule->schedule = ....
$schedule->ticket_header_ticket_id = ...

$schedule->save();

Notification::make('Updated')...
EL REKT
EL REKT5mo ago
ok, let me try sorry for stupid question, I'm still a beginner, in this case I want if there is a previous record from the $row results then update the record, if there isn't one then save a new record.. so is there a way to do like $schedule->update(['schedule' => $data['schedule']])->where('schedule_id', $row->schedule_id); and sorry for bad english.
Solution
Saade
Saade5mo ago
Schedule::updateOrCreate(
['ticket_header_ticket_id' => $ticket->ticket_id],
['schedule' => $data['schedule']]
)
Schedule::updateOrCreate(
['ticket_header_ticket_id' => $ticket->ticket_id],
['schedule' => $data['schedule']]
)
?
EL REKT
EL REKT5mo ago
thanks @Saade , let me try first it works! thanks again @Saade , i appreaciated your input. I use updateOrCreate function and adding unique field on migrations.