0

How to solve the error?

image

this is my htm code (button upload)

{{ form_open({files: true, request: 'onFileUpload'}) }}

<!--File Input-->
<input type="file" name="file-upload" required="required">
<!--File Input-->

<!--Submit/Upload Button-->
<button type="submit">Upload</button>

{{ form_close() }}

this is the component php code

     public function onFileUpload()
{
    $file = new System\Models\File;
    $file->data = Input::file('file-upload');
    $file->save();

    // Attach the uploaded file to your model
    $model->file()->add($file); 
   // The above line assumes you have proper attachOne or attachMany relationships defined on your model

    $model->file_path = url('/') . $model->file->getPath();
    $model->save();

    return Redirect::back();
}

is this the proper attachMany relationship?

public $attachMany = [
'files' => 'System\Models\File',
];
}    

I'm not very sure about the code cause I'm new to October cms Can anyone show some examples? How to create a drag and drop file uploader component?

BEX
  • 187
  • 4
  • 21
  • Just like the error says: you are trying to add something to $model->file(), but variable $model was never initiated. So $model is nothing at that point. – vrijdenker May 16 '16 at 08:10
  • I added this $model = new Uploadedfile;(<<< model) but still not working – BEX May 16 '16 at 09:11

1 Answers1

0

You need to new up an instance of the model that you've defined you relationship in and set it as your $model variable.

For example.. your model class could look like this.

class User extends Model
{
    public attachMany [
        'files' => 'System\Models\File'
    ];
}

And then in your component onFileUpload()

$model = new User;
$model->files()->add($file); 
$model->file_path = url('/') . $model->file->getPath();
$model->save();

Also, just take notice that your attachMany relationship in your model class is defined as files but you're trying to use it with

$model->file()->add();

It should be

$model->files()->add();