Array to string conversion:ErrorException when trying to store multiple files with FileUpload

public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->required()
->maxLength(255),
Textarea::make('description')
->required()
->maxLength(255),
TextInput::make('amount_in_euro')
->required()
->label("Amount")
->numeric(),
Select::make('purchaser')
->required()
->label('Purchaser')
->options(User::all()->pluck('name', 'id'))
->searchable(),
Select::make('status')
->label('Status')
->options(Status::all()->pluck('name', 'id'))
->searchable(),
DatePicker::make('date_of_purchase')
->required()
->displayFormat('d/m/Y'),
FileUpload::make('attachment')
->required()
->multiple()
->disk('public')
->storeFileNamesIn('attachment_file_names')
->enableDownload()
->maxSize(25600)
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->required()
->maxLength(255),
Textarea::make('description')
->required()
->maxLength(255),
TextInput::make('amount_in_euro')
->required()
->label("Amount")
->numeric(),
Select::make('purchaser')
->required()
->label('Purchaser')
->options(User::all()->pluck('name', 'id'))
->searchable(),
Select::make('status')
->label('Status')
->options(Status::all()->pluck('name', 'id'))
->searchable(),
DatePicker::make('date_of_purchase')
->required()
->displayFormat('d/m/Y'),
FileUpload::make('attachment')
->required()
->multiple()
->disk('public')
->storeFileNamesIn('attachment_file_names')
->enableDownload()
->maxSize(25600)
]);
}
23 Replies
Matthew
MatthewOP2y ago
This is the table:
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('test:expenses', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description');
$table->json('attachment');
$table->string('attachment_file_names');
$table->date("date_of_purchase");
$table->double('amount_in_euro');
$table->bigInteger('purchaser');
$table->bigInteger('status')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('alltrons:expenses');
}
};
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('test:expenses', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description');
$table->json('attachment');
$table->string('attachment_file_names');
$table->date("date_of_purchase");
$table->double('amount_in_euro');
$table->bigInteger('purchaser');
$table->bigInteger('status')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('alltrons:expenses');
}
};
Also, there is no array option in the table, so I was even more confused
toeknee
toeknee2y ago
You need to cast attachements to array in the model.
Matthew
MatthewOP2y ago
You mean, in the model?
toeknee
toeknee2y ago
That's what I said 🙂
Matthew
MatthewOP2y ago
oh oops 😂
Matthew
MatthewOP2y ago
Matthew
MatthewOP2y ago
I thought i did, but it was commented
toeknee
toeknee2y ago
😉 that'll be why it's coming into an array / string mix match
Matthew
MatthewOP2y ago
I still get the error 😅
toeknee
toeknee2y ago
Is this on edit?
Matthew
MatthewOP2y ago
on create
toeknee
toeknee2y ago
What happens when you create a new one
Matthew
MatthewOP2y ago
I changed the attachment from a string, to a json COuld that be the issue?
toeknee
toeknee2y ago
Possibly, you should nave a designated design path. So if you are using an select/multi-select you store in array usually. if you are using json then you need to cast to json, but I don't belive the editor natively supports date from a json dataset which would be why you are getting an array to string conversion
Matthew
MatthewOP2y ago
I changed to string again, and I still get the error I get the error
toeknee
toeknee2y ago
Ok no so you want Json, sorry I've jsut read the docs for multi-select on file upload
Matthew
MatthewOP2y ago
and I need to case to json as well? Yep, I also read it but I just thought it was the wrong choice in this case since I get an error OHH wait There is also an issue with the file names. Since you have multiple files, you also store multiple file names
toeknee
toeknee2y ago
Ok cool, but you shouldn't get string to array issue on that. You store as json as it's passed as json. There isn't a castor for JSON last time I checked
Matthew
MatthewOP2y ago
I still get the error. Hmm What are my next moves
Dan Harrin
Dan Harrin2y ago
please dd() the $data in mutateFormDataBeforeCreate
Matthew
MatthewOP2y ago
protected function mutateFormDataBeforeCreate(array $data): array
{
dd($data);
return $data;
}
protected function mutateFormDataBeforeCreate(array $data): array
{
dd($data);
return $data;
}
I still get the same error and nothing in the terminal Done! I fixed it
protected $casts = [
'attachment' => 'array',
'attachment_file_names' => 'array'
];

protected $casts = [
'attachment' => 'array',
'attachment_file_names' => 'array'
];

All i had to do is make a cast for attachment_file_names as well
toeknee
toeknee2y ago
yeah I missed storeFileNamesIn, that's good
moaaz_m_f
moaaz_m_f2y ago
APP_URL
Want results from more Discord servers?
Add your server