# Export user data

# Definition

Extracts all user data into a JSON file and send it to the user via email.

class ExportUserData
    use AsAction;

    public function handle(User $user): void
        // Extract and store user data as a JSON file.
        $content = json_encode($this->getAllUserData($user));
        $path = sprintf('user_exports/%s.json', $user->id);
        Storage::disk('s3')->replace($path, $content);

        // Send JSON file temporaty URL via email.
        Mail::to($user)->send(new UserDataExportReady($user, $path));

    protected function getAllUserData(User $user): array
        return [
            'profile' => $user->toArray(),
            'articles' => $user->articles->toArray(),

# Using as an object


# Using as an asynchronous job

You can use the dispatch static method instead of run to dispatch the actions as an asynchronous job.

Note that here we did not implement the asJob method since it would have been exactly the same as the handle method. In general, when no asX method is defined, the handle method is being used directly instead.


If you prefer defining the queue — or any other job settings — directly in the action, you can do this using the configureJob method.

use Lorisleiva\Actions\Decorators\JobDecorator;

class ExportUserData
    use AsAction;

    public function configureJob(JobDecorator $job): void

    // ...

# Using as a synchronous job

You can use the dispatchNow method to dispatch the action as a synchronous job.

Note that this is equivalent to using the action as an object using the run method.

Last Updated: 1/26/2022, 11:46:22 AM