title: Efficient way to populate drop-down dynamically in laravel
body: <p>I have multiple database tables which each one of them populates a dropdown, The point is each dropdown effects next dropdown.</p>
  
  <p>I mean if I select an item from the first dropdown, the next dropdown values change on the selected item, which means they are related and they populate dynamically on each dropdown item change.</p>
  
  <p>I know this is not efficient and need refactoring so I'd be glad to point me out to the right way of populating those dropdowns.</p>
  
  <p>This is the controller code:</p>
  
  <pre><code>&lt;?php
  
  use App\Http\Controllers\Controller;
  use App\Models\County;
  use App\Models\OutDoorMedia;
  use App\Models\Province;
  use Illuminate\Http\Request;
  use Illuminate\Validation\Rule;
  
  
  class FiltersController extends Controller
  {
      protected static $StatusCode = 0;
      protected static $Msg = '';
      protected static $Flag = false;
  
      public function index(Request $request)
      {
  
          try {
              $validator = Validator::make($request-&gt;all(), [
                  'province_id' =&gt; 'exists:province,id',
                  'county_id' =&gt; 'exists:county,id',
                  'media_type' =&gt; Rule::in([MEDIA_TYPE_BILLBOARD, MEDIA_TYPE_OUTDOOR_MONITOR, MEDIA_TYPE_INDOOR_MONITOR, MEDIA_TYPE_STAND, MEDIA_TYPE_STRABOARD, MEDIA_TYPE_BRAND_BOARD]),
                  'media_status' =&gt; Rule::in([MEDIA_STATUS_AVAILABLE, MEDIA_STATUS_NEGOTIATING, MEDIA_STATUS_ASSIGNED, MEDIA_STATUS_ARCHIVE]),
                  'category_id' =&gt; 'exists:category_list,id',
              ]);
  
              if ($validator-&gt;fails()) {
                  abort(400, $validator-&gt;errors()-&gt;first());
              } else {
                  //############################# Input Filters ####################################
                  $province_id = $request-&gt;has('province_id') ? $request-&gt;province_id : null;
                  $county_id = $request-&gt;has('county_id') ? $request-&gt;county_id : null;
  
                  $media_type = $request-&gt;has('media_type') ? $request-&gt;media_type : null;
                  $location = $request-&gt;has('location') ? $request-&gt;location : null;
                  $media_status = $request-&gt;has('media_status') ? $request-&gt;media_status : null;
  
  
                  $category_id = $request-&gt;has('category_id') ? $request-&gt;category_id : null;
  
                  //this flag is for detecting if user is requesting from "advertiser my order" to ignore some concitions
                  $advertiser_my_orders = ($request-&gt;has('my_orders') &amp;&amp; $request-&gt;my_orders == 'true') ? true : false;
  
                  $province_ids = [];
                  $county_ids = [];
  
                  //############################# Media Owner Filters ####################################
                  //offline section filters
                  if (!is_null($province_id) &amp;&amp; Province::whereId($request-&gt;province_id)-&gt;exists()) {
  
                      //check correction of county id
                      if (!is_null($county_id) &amp;&amp; County::whereId($county_id)-&gt;whereProvinceId($province_id)-&gt;exists()) {
  
                          $media_owner_ODM_Provinces = Province::whereHas('media')-&gt;get()-&gt;toArray();
  
                          $media_owner_ODM_Provinces_Counties = County::whereProvinceId($province_id)
                              -&gt;whereHas('county_media')-&gt;get()-&gt;toArray();
  
                          foreach ($media_owner_ODM_Provinces as $key =&gt; $province) {
                              if ($province['id'] == $province_id) {
                                  $media_owner_ODM_Provinces[$key]['county'] = $media_owner_ODM_Provinces_Counties;
                              }
                          }
  
                          $media_owner_ODM_locations = OutDoorMedia::whereProvinceId($province_id)-&gt;whereCountyId($county_id)-&gt;groupBy('location')-&gt;pluck('location')-&gt;toArray();
  
                          $media_owner_ODM_media_types = OutDoorMedia::whereProvinceId($province_id)-&gt;whereCountyId($county_id)-&gt;whereIn('location', $media_owner_ODM_locations)-&gt;groupBy('media_type')-&gt;pluck('media_type')-&gt;toArray();
  
                          $media_owner_ODM_media_Status = OutDoorMedia::whereProvinceId($province_id)-&gt;whereCountyId($county_id)-&gt;whereIn('media_type', $media_owner_ODM_media_types)-&gt;groupBy('status')-&gt;pluck('status')-&gt;toArray();
  
                      } else {
                          $media_owner_ODM_Provinces = Province::whereHas('media')-&gt;with(['county' =&gt; function ($query) {
                              $query-&gt;whereHas('county_media');
                          }])-&gt;get()-&gt;toArray();
  
                          $media_owner_ODM_locations = OutDoorMedia::whereProvinceId($province_id)-&gt;groupBy('location')-&gt;pluck('location')-&gt;toArray();
  
                          $media_owner_ODM_media_types = OutDoorMedia::whereProvinceId($province_id)-&gt;whereIn('location', $media_owner_ODM_locations)-&gt;groupBy('media_type')-&gt;pluck('media_type')-&gt;toArray();
  
                          $media_owner_ODM_media_Status = OutDoorMedia::whereProvinceId($province_id)-&gt;whereIn('media_type', $media_owner_ODM_media_types)-&gt;groupBy('status')-&gt;pluck('status')-&gt;toArray();
                      }
  
                  } else {
                      $media_owner_ODM_Provinces = Province::whereHas('media')-&gt;with(['county' =&gt; function ($query) {
                          $query-&gt;whereHas('county_media');
                      }])-&gt;get()-&gt;toArray();
  
                      foreach ($media_owner_ODM_Provinces as $province) {
                          $province_ids[] = $province['id'];
                          foreach ($province['county'] as $county) {
                              $county_ids[] = $county['id'];
                          }
                      }
  
                      $media_owner_ODM_locations = OutDoorMedia::whereIn('province_id', $province_ids)-&gt;whereIn('county_id', $county_ids)-&gt;groupBy('location')-&gt;pluck('location')-&gt;toArray();
  
                      $media_owner_ODM_media_types = OutDoorMedia::whereIn('province_id', $province_ids)-&gt;whereIn('county_id', $county_ids)-&gt;whereIn('location', $media_owner_ODM_locations)-&gt;groupBy('media_type')-&gt;pluck('media_type')-&gt;toArray();
  
                      $media_owner_ODM_media_Status = OutDoorMedia::whereIn('province_id', $province_ids)-&gt;whereIn('county_id', $county_ids)-&gt;whereIn('media_type', $media_owner_ODM_media_types)-&gt;groupBy('status')-&gt;pluck('status')-&gt;toArray();
                  }
  
                  $media_owner_offline = [
                      'provinces' =&gt; $media_owner_ODM_Provinces,
                      'media_status' =&gt; $media_owner_ODM_media_Status,
                      'location' =&gt; $media_owner_ODM_locations,
                      'media_type' =&gt; $media_owner_ODM_media_types,
                  ];
  
                  $filters['media_owner']['offline'] = $media_owner_offline;
  
                  self::$StatusCode = 200;
                  self::$Msg = $filters;
                  self::$Flag = true;
              }
          } catch (\Exception $e) {
              //=========== Get Error Exception Message ============
              self::$StatusCode = 400;
              self::$Msg = $e-&gt;getMessage();
              self::$Flag = false;
              return $this-&gt;CustomeJsonResponse(self::$Flag, self::$StatusCode, self::$Msg);
              //=========== Get Error Exception Message ============
          } finally {
              return $this-&gt;CustomeJsonResponse(self::$Flag, self::$StatusCode, self::$Msg);
          }
      }
  }
  </code></pre>
  
  <p>FYI: I'm using laravel 5.3 framework.</p>