spatie/laravel-personal-data-export

Usage instructions not working

LorenzoSapora opened this issue · 2 comments

Laravel: 5.8~
PHP: 7.3~
Nova: 2.6.1

I'm stuck on this section.

  • Added the routes to my routes/web.php
<?php
// Authentication Routes
Auth::routes(['verify' => true]);
Route::personalDataExports('personal-data-exports');
[...]
  • Added filesystem to config/filesystems.php
    'disks' => [
[...]
        'personal-data-exports' => [
            'driver' => 'local',
            'root' => storage_path('app/personal-data-exports'),
        ],
    ],
  • Added scheduler

  • Added to App\Models\User.php

<?php
namespace App\Models;
[...]
use Spatie\PersonalDataExport\ExportsPersonalData;

class User extends Authenticatable implements MustVerifyEmail, ExportsPersonalData
{
[...]
public function selectPersonalData(PersonalDataSelection $personalData): void {
    $personalData
        ->add('user.json', ['name' => $this->name, 'email' => $this->email])
        ->addFile(storage_path("avatars/{$this->id}.jpg"))
        ->addFile('other-user-data.xml', 's3');
}
public function personalDataExportName(string $realFilename): string {
    $userName = Str::slug($this->name);

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

At this point, it errors

Declaration of App\Models\User::selectPersonalData(App\Models\PersonalDataSelection $personalData): void must be compatible with Spatie\PersonalDataExport\Expor` 

And I'm not sure how to progress.

However, I do continue the usage instructions, just in case it requires something in a later step.

In a controller App\Http\Controllers\UserController.php

    public function requestData()
    {
        dispatch(new CreatePersonalDataExportJob(auth()->user());
        return view('user.yourdata');
    }

But I can't seem to shake that void error above.

Hi @LorenzoSapora,have you imported Spatie\PersonalDataExport\PersonalDataSelection? Since in your example you're only importing Spatie\PersonalDataExport\ExportsPersonalData.

Another thing: I've just updated the docs, the definition of personalDataExportName is wrong and should be changed to public function personalDataExportName(): string.

You're user model should look a bit like this:

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Str;
use Spatie\PersonalDataExport\ExportsPersonalData;
use Spatie\PersonalDataExport\PersonalDataSelection;

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

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

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

I can't get this package to work for me after following the usage instructions. I get the error:
Spatie\PersonalDataExport\PersonalDataSelection::forUser(): Argument #1 ($user) must be of type Spatie\PersonalDataExport\ExportsPersonalData, App\Models\User given, called in C:\xampp\htdocs\smartwealth\vendor\spatie\laravel-personal-data-export\src\Jobs\CreatePersonalDataExportJob.php on line 60