F
Filament2mo ago
Raphi

How can I ensure that only the user's data is exported?

There is a problem with other user data being downloaded. Only the data that really belongs to the user should be downloaded. How can I ensure this? Here is my Exporter Class
class ClientExporter extends Exporter
{
protected static ?string $model = Client::class;

public static function getColumns(): array
{
return [
ExportColumn::make('salutation'),
ExportColumn::make('gender'),
ExportColumn::make('firstname'),
ExportColumn::make('lastname'),
];
}

public static function getCompletedNotificationBody(Export $export): string
{
$body = 'Your client export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.';

if ($failedRowsCount = $export->getFailedRowsCount()) {
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.';
}

return $body;
}
class ClientExporter extends Exporter
{
protected static ?string $model = Client::class;

public static function getColumns(): array
{
return [
ExportColumn::make('salutation'),
ExportColumn::make('gender'),
ExportColumn::make('firstname'),
ExportColumn::make('lastname'),
];
}

public static function getCompletedNotificationBody(Export $export): string
{
$body = 'Your client export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.';

if ($failedRowsCount = $export->getFailedRowsCount()) {
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.';
}

return $body;
}
2 Replies
toeknee
toeknee2mo ago
Scope the client model by default to where user_id = auth()->id() ?
Raphi
Raphi2mo ago
That's what I do
class BelongsToUserScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*/
public function apply(Builder $builder, Model $model): void
{
if (auth()->check() && ! auth()->user()->isAdmin() ||
Filament::getCurrentPanel()->getId() == 'app') {
$builder->where('user_id', auth()->id());
}
}
}
class BelongsToUserScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*/
public function apply(Builder $builder, Model $model): void
{
if (auth()->check() && ! auth()->user()->isAdmin() ||
Filament::getCurrentPanel()->getId() == 'app') {
$builder->where('user_id', auth()->id());
}
}
}
Want results from more Discord servers?
Add your server