Php 带州和郊区的OctoberCMS Builder插件

Php 带州和郊区的OctoberCMS Builder插件,php,octobercms,octobercms-backend,octobercms-plugins,october-form-controller,Php,Octobercms,Octobercms Backend,Octobercms Plugins,October Form Controller,我已经使用插件创建了两个插件(States和suburbies),到目前为止,它运行得非常好 问题是,在州插件中,我只允许添加州名,而在郊区插件中,我允许用户先选择州,然后输入郊区名。到目前为止,这两个插件都可以正常工作 现在的问题是,我有第三个插件名为属性,其中我有两个下拉列表州和郊区,但现在所有州和所有郊区都显示出来了。但我希望用户首先选择州,然后根据州选择,它应该将其所有郊区都放到我的另一个郊区下拉列表中 我曾尝试使用Builder插件提供的dependsOn,但我无法理解基于当前场景一步

我已经使用插件创建了两个插件(Statessuburbies),到目前为止,它运行得非常好

问题是,在插件中,我只允许添加州名,而在郊区插件中,我允许用户先选择州,然后输入郊区名。到目前为止,这两个插件都可以正常工作

现在的问题是,我有第三个插件名为属性,其中我有两个下拉列表郊区,但现在所有州和所有郊区都显示出来了。但我希望用户首先选择,然后根据州选择,它应该将其所有郊区都放到我的另一个郊区下拉列表中

我曾尝试使用Builder插件提供的dependsOn,但我无法理解基于当前场景一步一步实现它的流程。下面是我迄今为止所做和尝试的代码

plugins\technobrale\properties\models\Property.php

<?php namespace Technobrave\Properties\Models;

    use Model;
    use technobrave\states\Models\State as State;
    use technobrave\suburbs\Models\Suburb as Suburb;

    public function getStateIdOptions()
        {
            // getting all states 

           $get_all_states = State::all();


           $fields[''] = 'Select any State';
           foreach ($get_all_states as $current_state) {
                $fields[$current_state->attributes['id']] = $current_state->attributes['state_name'];

           }
          ksort($fields);  
          return $fields;
        }    



        public function getSuburbIdOptions($stateId)
        {
            // getting all suburbs 

            $get_all_suburbs = Suburb::all();

            $fields[''] = 'Select any Suburb';
            foreach ($get_all_suburbs as $current_suburb) {
                $fields[$current_suburb->attributes['id']] = $current_suburb->attributes['suburb'];

            }       


          ksort($fields);  
          return $fields;
        }
    }
好的,伙计们

我终于想出了一个解决办法。这就是我所做的

fields.yaml文件:technobrare\properties\models\property\fields.yaml

        state_id:
            label: 'State:'
            span: auto
            required: 1
            type: dropdown
            tab: 'Address Information'
        suburb_id:
            label: 'Suburb:'
            span: auto
            required: 1
            type: dropdown
            tab: 'Address Information'
            placeholder: 'Select any Suburb'
            dependsOn: state_id
正如你在上面看到的

郊区id
中,我添加了两行代码

placeholder: 'Select any Suburb'
dependsOn: state_id
属性模型文件:technobrale\properties\models\Property.php

<?php namespace Technobrave\Properties\Models;

    use Model;
    use technobrave\states\Models\State as State;
    use technobrave\suburbs\Models\Suburb as Suburb;

    public function getStateIdOptions()
        {
            // getting all states 

           $get_all_states = State::all();


           $fields[''] = 'Select any State';
           foreach ($get_all_states as $current_state) {
                $fields[$current_state->attributes['id']] = $current_state->attributes['state_name'];

           }
          ksort($fields);  
          return $fields;
        }    



        public function getSuburbIdOptions($stateId)
        {
            // getting all suburbs 

            $get_all_suburbs = Suburb::all();

            $fields[''] = 'Select any Suburb';
            foreach ($get_all_suburbs as $current_suburb) {
                $fields[$current_suburb->attributes['id']] = $current_suburb->attributes['suburb'];

            }       


          ksort($fields);  
          return $fields;
        }
    }
在上面,我刚刚用下面的代码更新了getSuburbiOptions方法,并删除了我的旧代码

return Suburb::getNameList($this->state_id);
然后我去了我的郊区插件

郊区模型文件:technobrave\郊区\模型\郊区.php

<?php namespace Technobrave\Properties\Models;

    use Model;
    use technobrave\states\Models\State as State;
    use technobrave\suburbs\Models\Suburb as Suburb;

    public function getStateIdOptions()
        {
            // getting all states 

           $get_all_states = State::all();


           $fields[''] = 'Select any State';
           foreach ($get_all_states as $current_state) {
                $fields[$current_state->attributes['id']] = $current_state->attributes['state_name'];

           }
          ksort($fields);  
          return $fields;
        }    



        public function getSuburbIdOptions($stateId)
        {
            // getting all suburbs 

            $get_all_suburbs = Suburb::all();

            $fields[''] = 'Select any Suburb';
            foreach ($get_all_suburbs as $current_suburb) {
                $fields[$current_suburb->attributes['id']] = $current_suburb->attributes['suburb'];

            }       


          ksort($fields);  
          return $fields;
        }
    }
在这个模型文件中,我确保使用
belongsTo
并添加了getNameList方法,如下所示

 <?php namespace Technobrave\Suburbs\Models;

    use Model;
    use technobrave\states\Models\State as State;
    /**
     * Model
     */
    class Suburb extends Model
    {

        /**
         * @var string The database table used by the model.
         */
        public $table = 'youtable_here_';


        public $belongsTo = ['State' => [
                                                      'technobrave\states\Models\State',
                                                      'key' => 'state'
                                                  ],

                            ];


        /**
         * @var array Cache for nameList() method
         */
        protected static $nameList = [];

        public static function getNameList($stateId)
        {
            if (isset(self::$nameList[$stateId])) {
                return self::$nameList[$stateId];
            }

            return self::$nameList[$stateId] = self::wherestate($stateId)->lists('suburb', 'id');
        }

    }