F
Filament12mo ago
Quin.

file upload question

i really don't know how to describe this... I am new with the hole file upload. but here is my question! I am making a File upload when you upload your file , the file needs to get in a function like my code (Code below) It needs to get the file_get_contents, how can i do that?
public static function table(Table $table): Table
{
return $table->columns([
TextColumn::make('description'),
TextColumn::make('price'),
TextColumn::make('timestamp'),
])->headerActions([
Action::make('import')
->form([
FileUpload::make('file')
->reactive()
->visibility('private')
])
->label('Upload Transaction file')
->icon('heroicon-o-folder-open')
->handler([static::class, 'handleImport']),
]);
}

public static function handleImport(array $data): void
{
$file = $data['file'];
$file_get_contents($file)
public static function table(Table $table): Table
{
return $table->columns([
TextColumn::make('description'),
TextColumn::make('price'),
TextColumn::make('timestamp'),
])->headerActions([
Action::make('import')
->form([
FileUpload::make('file')
->reactive()
->visibility('private')
])
->label('Upload Transaction file')
->icon('heroicon-o-folder-open')
->handler([static::class, 'handleImport']),
]);
}

public static function handleImport(array $data): void
{
$file = $data['file'];
$file_get_contents($file)
4 Replies
toeknee
toeknee12mo ago
What are you trying to do with the file? But you would do something like:
public static function table(Table $table): Table
{
return $table->columns([
TextColumn::make('description'),
TextColumn::make('price'),
TextColumn::make('timestamp'),
])->headerActions([
Action::make('import')
->form([
FileUpload::make('file')
->reactive()
->visibility('private')
])
->action(function($data) {
$contents = file_get_contents($data['file']);
// Do what you want here, the baove might need you to specify the storage path too, but dd($data) to see what you get.
})
->label('Upload Transaction file')
->icon('heroicon-o-folder-open')
->handler([static::class, 'handleImport']),
]);
}

public static function handleImport(array $data): void
{
$file = $data['file'];
$file_get_contents($file)
public static function table(Table $table): Table
{
return $table->columns([
TextColumn::make('description'),
TextColumn::make('price'),
TextColumn::make('timestamp'),
])->headerActions([
Action::make('import')
->form([
FileUpload::make('file')
->reactive()
->visibility('private')
])
->action(function($data) {
$contents = file_get_contents($data['file']);
// Do what you want here, the baove might need you to specify the storage path too, but dd($data) to see what you get.
})
->label('Upload Transaction file')
->icon('heroicon-o-folder-open')
->handler([static::class, 'handleImport']),
]);
}

public static function handleImport(array $data): void
{
$file = $data['file'];
$file_get_contents($file)
Quin.
Quin.OP12mo ago
->action(function($data) {
$parser = new \Kingsquare\Parser\Banking\Mt940();
$engine = new Bunq();
$bunqs = file_get_contents($data['file']);
foreach ($bunqs as $bunq) {
foreach ($bunq->getTransactions() as $transaction) {
$price = $transaction->getPrice();
$description = $transaction->getDescription();
$entryTimestamp = $transaction->getEntryTimestamp();

$date = date('Y-m-d H:i:s', $entryTimestamp);

Transaction::create([
'price' => $price,
'description' => $description,
'timestamp' => $date,
]);
}
}
})
->action(function($data) {
$parser = new \Kingsquare\Parser\Banking\Mt940();
$engine = new Bunq();
$bunqs = file_get_contents($data['file']);
foreach ($bunqs as $bunq) {
foreach ($bunq->getTransactions() as $transaction) {
$price = $transaction->getPrice();
$description = $transaction->getDescription();
$entryTimestamp = $transaction->getEntryTimestamp();

$date = date('Y-m-d H:i:s', $entryTimestamp);

Transaction::create([
'price' => $price,
'description' => $description,
'timestamp' => $date,
]);
}
}
})
something like this , first a made it in a controller but i want the file to be uploaded by the filament panel but it gets not the content of the file when i upload Nevermind got it fixed! stupid mistake off me, didn't know where its stored but now i changed that in the file_get_content like this
file_get_contents(storage_path('app/public/' . $data['file']))
file_get_contents(storage_path('app/public/' . $data['file']))
toeknee
toeknee12mo ago
FYI a faster approach would be to build the data into an array and just INSERT it unless you need to have watchers etc on the create.
Quin.
Quin.OP12mo ago
Thank you man will have a futher look into it to learn it to it's full opportunity
Want results from more Discord servers?
Add your server