53

Has anyone here ever created a nested dropdown within the backpack's cruds?

I've got this crud controller which handles the 'Campaign' model -

$this->crud->addField([
        'name' => 'industry_id',
        'entity' => 'industry',
        'type' => 'select2',
        'attribute' => 'name',
        'label' => "Industry",
    ]);
    $this->crud->addField([
        'name' => 'country_id',
        'entity' => 'country',
        'type' => 'select2',
        'attribute' => 'country_name',
        'label' => "Country",
    ]);

    $this->crud->addField([
        'name' => 'website_id',
        'entity' => 'website',
        'type' => 'select2',
        'attribute' => 'name',
        'label' => "Website",
    ]);
    $this->crud->addField([
        'name' => 'campaign_type_id',
        'entity' => 'campaign_type',
        'type' => 'select2',
        'attribute' => 'name',
        'label' => "Campaign Type",
    ]);

    $this->crud->addField([
        'name' => 'site_page_id',
        'entity' => 'site_page',
        'type' => 'select2',
        'attribute' => 'name',
        'label' => "Site Page",
    ]);

The 'website' entity depends on the industry field, and the 'site_page_id' depends on the cross between the 'website' entity and the 'campaign_type' entity.

When I'm creating a new campaign, I'm willing the dropdowns to be filled up dynamically according to the selected value of the dropdown above them.

I tried adding a method to the campaign's model as suggested in this pull and call it within the crud field's but it didn't work.

The only solution I can think of now is creating a custom create.blade.php file and call it with $this->crud->setCreateView('vendor.backpack.base.new_create'); from campaign's model setup() method.

blue pine
  • 472
  • 6
  • 17
Gonras Karols
  • 1,150
  • 10
  • 30
  • 1
    Gonras, that PR _should_ work, I’ve tried it many times. The only thing it doesn’t do is clear subsequent inputs. – tabacitu Nov 01 '18 at 07:07
  • Take note you’d need to use select2_from_ajax, not select2. – tabacitu Nov 01 '18 at 07:08
  • Rather than create a new create.blade.php file you could create a custom files type that includes all 3 fields you need - that should be easier. – tabacitu Nov 01 '18 at 07:08
  • https://backpackforlaravel.com/docs/3.4/crud-fields#creating-a-custom-field-type – tabacitu Nov 01 '18 at 07:09

1 Answers1

1

You can use $this->crud->with(); or $this->crud->with(['website', 'site_page']); It will get all the duplicate data in the relationships.

Let me know if this helped

Jordan
  • 21
  • 3