OctoberCMS Builder Plugin backend dropdown
I would like to create a plugin where to create
Posts, each post belong to 1
And there is a dropdown menu at the backend where you can choose which category the post belongs to when you are creating a post.
However, I want the the options in such dropdown depend on the model
Category where the
category.id instead of me typing the string into it.
Please help! Thanks
Thanks to Dragontree's suggestions: here is my 1st EDIT:
I already have my relationship set as followings:
public $hasMany = [ 'posts' => ['stephenli\plugin\Models\Posts'] ];
public $belongsTo = [ 'category' => ['stephenli\plugin\Models\Category', 'key' => 'category_id'] ]; /* I tried with or without 'key' => 'category_id'*/
and used relation widget at the
category_id: label: 'stephenli.postgod::lang.plugin.categorypick_label' oc.commentPosition: '' nameFrom: name descriptionFrom: description emptyOption: 'No categories found' span: auto type: relation
category_id: label: 'stephenli.plugin::lang.plugin.categorypick_label' type: text searchable: true sortable: true
When I access to my
Post Plugin and click the
Create button or try to amend any existing record in
list, the following error shows:
Model 'plugin\Models\Post' does not contain a definition for 'category_id'.
To sum up everything for any other people who encounter the same issue in the future, there are two methods to solve the problem
You can either
Dropdownwidget and change the options with detailed solution here provided by @Mittul At TechnoBrave
- Or directly use
Relationwidget as provided in the suggested solution by @dragontree
There are several ways to define the dropdown options.
But in this case its sounds like you should be using the
relationwidget instead on a normal dropdown:
category: label: Category type: relation nameFrom: name
PS. This also requires you to define the relations in the
Category models. But you should do that anyway.
It is also worth noting that the field name (in fields.yaml) should match the name of the relation.
- → OctoberCMS Backend Loging Hash Error
- → "failed to open stream" error when executing "migrate:make"
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → Create plugin that makes objects from model in back-end
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → How to install console for plugin development in October CMS
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → How to update data attribute on Ajax complete
- → October CMS - Conditionally Load a Different Page