how to update another model instead of updating current resource?

hello i want to insert data into another model first when user edit a form. what should i do?
7 Replies
matin rajabi
matin rajabi2mo ago
how to tell this function don't update any field?
Povilas K
Povilas K2mo ago
Maybe use Eloquent observers with updating() method (not updated()) that would execute before the update.
LeandroFerreira
LeandroFerreira2mo ago
sorry, what does it mean?
<Λmir>
<Λmir>2mo ago
I recommend handle this in the Model level. You may use an Eloquent observer and the updating/updated method. You also can check weather specific field has been updated by using wasChanged.
public function updated(Post $post): void
{
if ($post->wasChanged('title')) {
// Title field has been updated.
}
}
public function updated(Post $post): void
{
if ($post->wasChanged('title')) {
// Title field has been updated.
}
}
Note: The updating/updated methods will not called at all when no fields has been changed.
dissto
dissto2mo ago
I suppose you could do something like this on your EditXXX class: (perhaps wrap it in a db transaction)
public function save(bool $shouldRedirect = true): void
{
// dd($this->data);
// do something here

parent::save($shouldRedirect);

}
public function save(bool $shouldRedirect = true): void
{
// dd($this->data);
// do something here

parent::save($shouldRedirect);

}
matin rajabi
matin rajabi2mo ago
mamnun 😃 🤝 thanks for your help thanks for replying. problem solved thanks