Which package are you using to create the forms ?
Please help: Attachments( uploads) for multiple models - MorphToMany Filamentphp
I am developing an app under filementphp 2.x. I will have many models which will need attachments (images, pdfs, xls, docx, etc.) so I need to make a polymorphic relationship such as fileable in order not to repeat tables and tables with files fields.
I have created the morph files table, as this migration:
Schema::create('files', function (Blueprint $table) {
$table->id();
$table->morphs('fileable');
$table->string('file_name')->nullable();
$table->string('original_file_name')->nullable();
$table->timestamps();
});
I have put the fileable function in my FILE Model file:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class File extends Model
{
use HasFactory;
public function fileable()
{
return $this->morphTo();
}
}
I have put the files function in my model file (Model is called TiposFinca):
class TiposFinca extends Model
{
use HasFactory, SoftDeletes;
protected static ?string $name = 'Tipos de finca';
public $fillable = [
'nombre', 'coste', 'files'
];
public function files()
{
return $this->morphMany(File::class, 'fileable');
}
}
And now, for my create form, I have added to my TiposFincaResource.php file a new FileUpload field:
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('nombre')
->autofocus()
->required()
->unique(ignoreRecord: true)
->placeholder(__('Nombre')),
FileUpload::make('files')->multiple()->preserveFilenames(),
...
But when I try to save the new record, it throws an error saying:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'files' in 'field list'
I know it really is NOT a field in that table. What is must do is save a new record on the table "Files" but it does not do that.
Any idea, please?
Thanks in advance.
Please or to participate in this conversation.