This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Mraag
Mraag

Hello I am new to OctoberCMS and I am having the following problem:

I am trying to create a model with some image attached to it but I don't get it to work. This is my code:

My Model:

class Tree extends Model {
    public $table = 'myplugin_trees_trees';

    public $attachOne = [
        'avatar' => ['System\Models\File']
    ];

    public $dates = ['cut_at', 'dries_from', 'dries_to'];
}

My migration file:

class CreateTreesTable extends Migration {
    public function up() {
        Schema::create('myplugin_trees_trees', function($table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('name');
            $table->longText('description');
        });
    }

    public function down()  {
        Schema::dropIfExists('myplugin_trees_trees');
    }
}

And finally my controller:

class Trees extends \Backend\Classes\Controller {
    public $implement = [
        'Backend.Behaviors.FormController',
        'Backend.Behaviors.ListController',
    ];

    public $formConfig = 'config_form.yaml';
    public $listConfig = 'config_list.yaml';

    public function __construct() {
        parent::__construct();

        BackendMenu::setContext('MyPlugin.Trees', 'trees', 'trees');

    }

    public function index(){
        $this->asExtension('ListController')->index();
    }

    public function create() {
        $this->bodyClass = 'compact-container';
        return $this->asExtension('FormController')->create();
    }
}

In my fields.yaml I have the following code:

    avatar:
        label: Avatar
        type: fileupload
        mode: image
        imageHeight: 260
        imageWidth: 260

I can add add a tree to the database but the attached file will not show up in the database records (I tried to add a 'attachments' field to the trees table as said in the documentation but this didn't work either). I don't get any warnings or errors when submitting the form to the database.

What am I missing?

Last updated

FelixINX
FelixINX

There is no avatar collumn in your migration file. You should put it, and then you can attach file.

Example: Migration file:

class CreateTreesTable extends Migration {
    public function up() {
        Schema::create('myplugin_trees_trees', function($table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('name');
            $table->string('avatar');      #Avatar collumn
            $table->longText('description');
        });
    }

    public function down()  {
        Schema::dropIfExists('myplugin_trees_trees');
    }
}

Model file is the same.

myst6re
myst6re

I think this should be documented, there no information about a string column to add in http://octobercms.com/docs/database/attachments

joseph.w.crowell30463
joseph.w.crowell30463

You shouldn't add this string column. That will break the file relation and attempt to access this string instead of the relation.

LucianDex
LucianDex

As Josheph writted above, there's no need to add a column for file/files attachements. Read this, specially the NOTE. :) https://octobercms.com/docs/database/attachments#file-attachments

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.