F
Filament14mo ago
harps

mutateFormDataBeforeCreate() when creating in modal

I have a function in the create class but wanted to know how I can use the same in a modal. In the docs all the life cycle hooks are in the create class too so I assume they don't work with modals either?
2 Replies
wyChoong
wyChoong14mo ago
That would be just basic php function You can put your logic in any class static function. Then call in the create or modal
harps
harpsOP14mo ago
I have the following:
<?php

namespace App\Filament\Resources\ProjectResource\Pages;

use App\Filament\Resources\ProjectResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Facades\Storage;

class CreateProject extends CreateRecord
{
protected static string $resource = ProjectResource::class;

protected function mutateFormDataBeforeCreate(array $data): array
{
// Get the next auto increment from the datahase.
$nextAutoIncrement = ProjectResource::getModel()::max('id') + 1;
// Move the file from the temporary storage to the private storage.
Storage::disk('private')->move('project-shapes/' . $data['shape_file'], 'project-shapes/' . $nextAutoIncrement . '/' . $data['shape_file']);
// Change the file path to be the next auto increment.
$data['shape_file'] = 'project-shapes/' . $nextAutoIncrement . '/' . $data['shape_file'];

return $data;
}
}
<?php

namespace App\Filament\Resources\ProjectResource\Pages;

use App\Filament\Resources\ProjectResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Facades\Storage;

class CreateProject extends CreateRecord
{
protected static string $resource = ProjectResource::class;

protected function mutateFormDataBeforeCreate(array $data): array
{
// Get the next auto increment from the datahase.
$nextAutoIncrement = ProjectResource::getModel()::max('id') + 1;
// Move the file from the temporary storage to the private storage.
Storage::disk('private')->move('project-shapes/' . $data['shape_file'], 'project-shapes/' . $nextAutoIncrement . '/' . $data['shape_file']);
// Change the file path to be the next auto increment.
$data['shape_file'] = 'project-shapes/' . $nextAutoIncrement . '/' . $data['shape_file'];

return $data;
}
}
But it only works when the createProject is on a route not in a modal. Works
public static function getPages(): array
{
return [
'index' => Pages\ListProjects::route('/'),
'create' => Pages\CreateProject::route('/create'),
'view' => Pages\ViewProject::route('/{record}'),
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListProjects::route('/'),
'create' => Pages\CreateProject::route('/create'),
'view' => Pages\ViewProject::route('/{record}'),
];
}
Does not work
public static function getPages(): array
{
return [
'index' => Pages\ListProjects::route('/'),
'view' => Pages\ViewProject::route('/{record}'),
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListProjects::route('/'),
'view' => Pages\ViewProject::route('/{record}'),
];
}
Cheers Dan
Want results from more Discord servers?
Add your server