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
Scope the client model by default to where user_id = auth()->id() ?
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());
}
}
}