You should use Model Observer for this scenario, https://nova.laravel.com/docs/3.0/resources/#resource-events
Feb 16, 2021
3
Level 2
Can I preform a Nova Action on Creation or Update of a Resource?
I have a Page resource in Nova that has an is_homepage boolean. If a User marks the Page as homepage true I would like to perform an action on saving or update in order to mark any other pages in the database that were previously set to the homepage as false?
What is the best way to accomplish this?
I'd really like to Query all other pages in the DB and perform the action before I update or save the new page as the new homepage.
Here are the steps I would like to perform.
/// User is creating or updating a Page resource
Boolean::make('Is Homepage') //sets to true
///User clicks create or update and just before update or save the following function runs
function() {
$pages = Page::where('is_homepage', true)->get();
foreach($pages as $page)
{
$page->is_homepage = false;
$page->save();
)
}
//after this action runs the update to the new or existing object is preformed
Please or to participate in this conversation.