PHP code example of spatie / laravel-personal-data-export

1. Go to this page and download the library: Download spatie/laravel-personal-data-export library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / laravel-personal-data-export example snippets


// somewhere in your app

use Spatie\PersonalDataExport\Jobs\CreatePersonalDataExportJob;

// ...

dispatch(new CreatePersonalDataExportJob(auth()->user()));

// in your User model

public function selectPersonalData(PersonalDataSelection $personalDataSelection): void {
    $personalDataSelection
        ->add('user.json', ['name' => $this->name, 'email' => $this->email])
        ->addFile(storage_path("avatars/{$this->id}.jpg"))
        ->addFile('other-user-data.xml', 's3');
}

public function selectPersonalData(PersonalDataSelection $personalDataSelection): void {
    $personalDataSelection
        ->addFile(storage_path("avatars/{$this->id}.jpg"), directory: 'avatars');
}

// in your routes file

Route::personalDataExports('personal-data-exports');

// in config/filesystems.php

// ...

'disks' => [

    'personal-data-exports' => [
        'driver' => 'local',
        'root' => storage_path('app/personal-data-exports'),
    ],

// ...

// app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
   $schedule->command('personal-data-export:clean')->daily();
}

php artisan vendor:publish --provider="Spatie\PersonalDataExport\PersonalDataExportServiceProvider" --tag="personal-data-export-config"

return [
    /*
     * The disk where the exports will be stored by default.
     */
    'disk' => 'personal-data-exports',

    /*
     * The amount of days the exports will be available.
     */
    'delete_after_days' => 5,

    /*
     * Determines whether the user should be logged in to be able
     * to access the export.
     */
    'authentication_' => [
        'queue' => null,
        'connection' => null,
    ],
];


namespace Spatie\PersonalDataExport;

interface ExportsPersonalData
{
    public function selectPersonalData(PersonalDataSelection $personalDataSelection): void;

    public function personalDataExportName(): string;
}

// in your user model

public function selectPersonalData(PersonalDataSelection $personalDataSelection): void {
    $personalDataSelection
        ->add('user.json', ['name' => $this->name, 'email' => $this->email])
        ->addFile(storage_path("avatars/{$this->id}.jpg"))
        ->addFile('other-user-data.xml', 's3');
}

// on your user

public function personalDataExportName(): string {
    $userName = Str::slug($this->name);

    return "personal-data-{$userName}.zip";
}

// somewhere in your app

use Spatie\PersonalDataExport\Jobs\CreatePersonalDataExportJob;

// ...

dispatch(new CreatePersonalDataExportJob(auth()->user()));

use Spatie\PersonalDataExport\Notifications\PersonalDataExportedNotification as SpatiePersonalDataExportedNotification;
class PersonalDataExportedNotification extends SpatiePersonalDataExportedNotification
{
    public function via($notifiable)
    {
        return ['mail'];
    }
    public function toMail($notifiable)
    {
        $downloadUrl = route('personal-data-exports', $this->zipFilename);
        if (static::$toMailCallback) {
            return call_user_func(static::$toMailCallback, $notifiable, $downloadUrl);
        }
        return (new MailMessage)
            ->subject(trans('personal-data-exports.notifications.subject'))
            ->line(trans('personal-data-exports.notifications.instructions'))
            ->action(trans('personal-data-exports.notifications.action'), $downloadUrl)
            ->line(trans('personal-data-exports.notifications.deletion_message', ['date' => $this->deletionDatetime->format('Y-m-d H:i:s')]));
    }
}

    /*
     * Something like that
     */
    'notification' => \App\Notifications\PersonalDataExportedNotification::class,

use Spatie\PersonalDataExport\Jobs\CreatePersonalDataExportJob;

class MyCustomJobClass extends CreatePersonalDataExportJob
{
    public $queue = 'my-custom-queue';
}

dispatch(new MyCustomJobClass(auth()->user()));
bash
php artisan vendor:publish --provider="Spatie\PersonalDataExport\PersonalDataExportServiceProvider" --tag="personal-data-export-translations"