0
votes

I have a Laravel Nova backend in which I want users to be able to drop large sets of records by filter selection. Those records have some relationships with other models as well and those need to be cleaned. For example, an admin may want to drop all users that don't have any roles and detach them from the groups they are in and remove all the posts they made. This could be some 25000 users.

It appears to me that because of the filter selection, Nova is processing this request line by line which eventually leads to a timeout on the server. I want to send a query to the database to remove all users who's id is in the filter selection. MySQL will handle the removing of the relationships by cascading. This is far quicker than the row by row processing of Nova.

But I cannot figure out how to override the handling of the delete request by Nova. The closest I've managed to get is tapping into the deleting event of the model but by then Nova is already handling the request line by line. How do I hook up my own controller for this request?

1

1 Answers

0
votes

Instead of overriding delete create an action to do the work.

After filtering and selecting the list of Users (or whatever model you need to delete) call the action from the drop-down list. In this screen shot from the Nova docs they have a "Email Account Profile" action.

Action selection from Nova documentation

The handle method of the action is passed a collection of the selected models

/**
* Perform the action on the given models.
*
* @param  \Laravel\Nova\Fields\ActionFields  $fields
* @param  \Illuminate\Support\Collection  $models
* @return mixed
*/
public function handle(ActionFields $fields, Collection $models)
{
    foreach ($models as $model) {
      ...

You can either iterate over the collection deleting each one or use the model keys to build a SQL query and delete them in one go.

You can even queue the actions if you want to process the action in the background to avoid timeouts.