bnbwebexpertise/laravel-attachments

additional fields in attachment model

phoenixgao opened this issue · 2 comments

Hi,

I'm trying to add a custom additional field(s) into attachments table,
so first I create a migration to add those fields,

then in the dropzone form I add those input elements

then in my controller:
(I try to attach the UploadedFile directly to the model instead of uploading to dropzone controller first then bind uuid)

$attachment = $user->attach(
    $request->file($request->input('file_key', 'file')), // <== the UploadedFile instance
    array_merge(array_only($request->input(), [
        'title',
        'description',
        'category', //  <== here is the additional field
        'key',
    ]), [
        'metadata' => ['dz_session_key' => csrf_token()], // <== actually this also got ignored
    ]) . // <== the $options argument
);

but this got ignored because

https://github.com/bnbwebexpertise/laravel-attachments/blob/master/src/Attachment.php#L77

so I'm wondering is it necessary to filter out fields other than ['title', 'description', 'key', 'disk'] ?
or was I doing something wrong?
what is the easiest way to achieve what I'm trying to do?

Thanks!!

Hi !

If we do not filter the options array we would get an error like this :

Column not found: 1054 Unknown column 'foo' in 'field list' (SQL: update attachments set model_id = 1, model_type = App\User, metadata = [], updated_at = 2017-07-05 16:32:01, foo = bar where id = 22)

In order to make the attachment model extendable we need to provide a way to declare those additionnals columns and used them in the filter call.

Maybe we could declare an attributes array in config/attachements.php with the default ones. And then it would be up to the developper to tune this setting.

You can try to publish the package config and edit the attributes array in the config/attachments.php file.

We've added info at the end of the README.

Available in version 0.0.13