How can I upload file with UUID directory

I want the uuid field is same with the upload directory, but after I see the log, the debug log will print twice after I added FileUpload, and generate the UUID again, this make the UUID in upload directory is different to uuid field.
public static function form(Form $form): Form
{
$uuid = (string) Str::uuid();

Log::debug('form');
Log::debug($uuid);

return $form
->schema([
Forms\Components\Hidden::make('uuid')
->default($uuid)
->visibleOn('create'),

Forms\Components\FileUpload::make('video')
->directory("contents/{$uuid}"),
]);
}
public static function form(Form $form): Form
{
$uuid = (string) Str::uuid();

Log::debug('form');
Log::debug($uuid);

return $form
->schema([
Forms\Components\Hidden::make('uuid')
->default($uuid)
->visibleOn('create'),

Forms\Components\FileUpload::make('video')
->directory("contents/{$uuid}"),
]);
}
[2023-06-12 14:06:45] local.DEBUG: form
[2023-06-12 14:06:45] local.DEBUG: 6078e6d2-74b9-4356-baec-c7fdc0dfcd24
[2023-06-12 14:06:45] local.DEBUG: form
[2023-06-12 14:06:45] local.DEBUG: 6abe8fb4-fef8-4f86-8b6c-2f938201e983
[2023-06-12 14:06:45] local.DEBUG: form
[2023-06-12 14:06:45] local.DEBUG: 6078e6d2-74b9-4356-baec-c7fdc0dfcd24
[2023-06-12 14:06:45] local.DEBUG: form
[2023-06-12 14:06:45] local.DEBUG: 6abe8fb4-fef8-4f86-8b6c-2f938201e983
Solution:
Found a solution. I don't know if it is the best way, but at least it can accomplish my purpose, write it down in case anyone also needs this. ```php public static function form(Form $form): Form...
Jump to solution
4 Replies
Dennis Koch
Dennis Koch14mo ago
Use memoization. The form method it called multiple times leading to multiple UUIDS
.ddddddddddddddddddddddddddddddd
I tried spatie/once, but the UUIDs are still different.
class MyClass
{
public static function getUuid()
{
return once(function () {
return (string) Str::uuid();
});
}
}
class MyClass
{
public static function getUuid()
{
return once(function () {
return (string) Str::uuid();
});
}
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Hidden::make('uuid')
->default(MyClass::getUuid())
->visibleOn('create'),

Forms\Components\FileUpload::make('video')
->directory('contents/' . MyClass::getUuid()),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Hidden::make('uuid')
->default(MyClass::getUuid())
->visibleOn('create'),

Forms\Components\FileUpload::make('video')
->directory('contents/' . MyClass::getUuid()),
]);
}
Dennis Koch
Dennis Koch14mo ago
Hm, weird. I don't know, sorry.
Solution
.ddddddddddddddddddddddddddddddd
Found a solution. I don't know if it is the best way, but at least it can accomplish my purpose, write it down in case anyone also needs this.
public static function form(Form $form): Form
{
$uuid = (string) Str::uuid();

return $form
->schema([
Forms\Components\Hidden::make('uuid')
->default($uuid)
->visibleOn('create'),

Forms\Components\FileUpload::make('video')
->saveUploadedFileUsing(function ($file, $get, $set) {
return $file->storeAs("contents/{$get('uuid')}", $file->getClientOriginalName());
}),
]);
}
public static function form(Form $form): Form
{
$uuid = (string) Str::uuid();

return $form
->schema([
Forms\Components\Hidden::make('uuid')
->default($uuid)
->visibleOn('create'),

Forms\Components\FileUpload::make('video')
->saveUploadedFileUsing(function ($file, $get, $set) {
return $file->storeAs("contents/{$get('uuid')}", $file->getClientOriginalName());
}),
]);
}