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

TonUK
TonUK

My data is straight forward. I have 3 models for managing vehicles.
Make (e.g. BMW)
Model (e.g 1 Series)
Vehicle (Variation) (e.g. spec name/derivative "120d Sport " and then all the spec data of the vehicle)

So the Model belongs to a Make. And the vehicle itself belongs to a Model and a Make.

In my create Vehicle form I have the 2 relation fields set up (Make & Model) which uses a drop down for the 1-Many relationship. I know I can hide the model drop down until a make is selected first, but at the moment every model in the database is displayed and selectable. I am struggling to work out how to get the list of models to update based on the selected make and return only models belonging to that make.

I've read through some docs and have previously used the getModelOptions method to populate a dropdown list. I think I can use the dependsOn trigger method even on relation fields, but unsure how to then use the filterFields or getModelOptions or a combination of both, in order to retrieve only models belonging to the selected Make.

All advice and pointers much appreciated. Thanks.

Last updated

mjauvin
mjauvin
mjauvin

Try something like this:

public function getModelOptions()  {
   if (post('make')) {
      $make = Make::find(post('make'));
      return $make->models->lists('name', 'id');
   } else {
      return Model::all()->lists('name', 'id');
   }
}
mjauvin
mjauvin

Actually, this should be:

public function getCarModelOptions()  {
   if ($this->carMake) {
      return $this->carMake->makeModels->lists('name', 'id');
   } else {
      return CarModel::all()->lists('name', 'id');
   }
}

And make sure carModel dependsOn carMake in your field definition.

mjauvin
mjauvin

If both your Make / Model field is of type relation in your Vehicle field definition file, you can also add a scope to the Model field definition as shown below:

car_make:
   type: relation
car_model:
   type: relation
   scope: filterMake
   dependsOn: car_make

And add this method to the CarModel Model:

public function scopeFilterMake($query, $parent)
{
   if ($parent->car_make) {
      $query = $query->where('make_id', $parent->make_id);
   }
   return $query;
}
TonUK
TonUK

mjauvin said:

Actually, this should be:

public function getCarModelOptions()  {
  if ($this->carMake) {
     return $this->carMake->makeModels->lists('name', 'id');
  } else {
     return CarModel::all()->lists('name', 'id');
  }
}

And make sure carModel dependsOn carMake in your field definition.

Thank you very much @mjauvin. This worked for me. Had to change the field type from relation to dropdown though, but it does the trick.

Last updated

TonUK
TonUK

One thing I ain't sure about is hiding the model field until a brand it selected. I know I can add the trigger which watches the Make/Brand field...but since it isnt a check box I can't used the checked value.....and since I want to just check for any value I'm not sure what to put in the value[] option.....

**EDIT Looks like I can use the filtering form fields for the hiding/showing of the model field based on if the Brand/Make is selected.

Last updated

mjauvin
mjauvin

I personally use this:

    car_model:
        label: Model
        type: relation
        scope: filterMake
        placeholder: -- select model --
        dependsOn: car_make
        trigger:
            action: hide
            field: car_make
            condition: value[]
mjauvin
mjauvin

Make sure you set the emptyOption and/or placeholder for the car_make field in order for this to work.

No need for filterFields

Last updated

TonUK
TonUK

mjauvin said:

Make sure you set the emptyOption and/or placeholder for the car_make field in order for this to work.

No need for filterFields

Again, thank you for the help. Much appreciated.

1-10 of 10

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