OctoberCMS Builder plugin with state and suburb

Ad

I have created two plugins (States and Suburbs) using Builder plugin and so far it works great.

The Thing is, in States plugin, I am simply allowing ability to add state names and in Suburbs plugin, I am allowing users to select State first and then enter suburb name. All works fine as of now in these two plugins.

Now the thing is, I have a third plugin called as Properties in which I have both these 2 drop-downs State and Suburb but as of now All States and All Suburbs are showing. But I want users to select first State and then based on state selection it should should all its suburbs to my another Suburb drop-down.

I have tried to use dependsOn which Builder plugin provides but I just am not able to understand the flow to achieve it step by step based on the current scenario I have. Below is the code what I have done and tried so far.

plugins\technobrave\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;
        }
    }

How can I do this from here on ? Any help or guidance will be highly appreciated and useful.

Thanks

Ad

Answer

Ad

Ok Guys,

Eventually I have come up with a solution. Here is what I have done.

fields.yaml File: technobrave\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

As you can see above,

In suburb_id I have added, below two lines of code.

placeholder: 'Select any Suburb'
dependsOn: state_id

Property Model File: technobrave\properties\models\Property.php

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()
{
    return Suburb::getNameList($this->state_id);
}

Here above, I have just updated getSuburbIdOptions method with below code and removed my old code.

return Suburb::getNameList($this->state_id);

Then I went to my Suburbs plugin.

Suburb Model File: technobrave\suburbs\models\Suburb.php

And in this model file, I have made sure to use belongsTo and added getNameList method like below.

 <?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');
        }

    }

And it works.

Hope it will help to those who stuck on the same issue/implementation.

Thanks

Ad
source: stackoverflow.com
Ad