Populating nested dropdowns using backpack for Laravel
Asked Answered
H

1

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.

Headsman answered 24/10, 2018 at 16:42 Comment(4)
Gonras, that PR should work, I’ve tried it many times. The only thing it doesn’t do is clear subsequent inputs.Patrick
Take note you’d need to use select2_from_ajax, not select2.Patrick
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.Patrick
backpackforlaravel.com/docs/3.4/…Patrick
G
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

Garman answered 15/9, 2022 at 2:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.