使用laravel 5.2 Backpack从select_from_array字段中获取所选值

时间:2016-09-23 11:54:29

标签: php laravel-5.2 backpack-for-laravel

我在我的新项目中使用Laravel 5.2 Backpack,我的表单中有一个select_from_array字段,具体取决于我希望数据在另一个select_from_array字段中显示的选定值。不知道该怎么做。请帮我解决一下这个。这是我的代码

Controller.php这样

public function __construct()
{
    if (Request::segment(3) == 'create') {
        $parentField1 = [
        'name' => 'cat_id',
        'label' => 'Category',
        'type' => 'select_from_array',
        'options' => $this->categories(),
        'allows_null' => false,
        ];
        $parentField = [
            'name' => 'subCat_id',
            'label' => 'SubCategory',
            'type' => 'select_from_array',
            'options' => $this->subcategories(),
            'allows_null' => false,
        ];

        array_unshift($this->crud['fields'], $parentField1,$parentField);

    }

public function categories()
{
    $cat = Request::get('cat_id');

    $currentId = 0;
    if (Request::segment(4) == 'edit' and is_numeric(Request::segment(3))) {
        $currentId = Request::segment(3);
    }

    $entries = Category::where('translation_lang', config('app.locale'))->where('parent_id',0)->orderBy('lft')->get();
    if (is_null($entries)) {
        return [];
    }

    $tab = [];
    $tab[0] = 'Root';
    foreach ($entries as $entry) {
        if ($entry->id != $currentId) {
            $tab[$entry->translation_of] = '- ' . $entry->name;
        }
    }
    return $tab;
}

public function subcategories()
{
    $currentId = 0;

    if (Request::segment(4) == 'edit' and is_numeric(Request::segment(3))) {
        $currentId = Request::segment(3);
    }

    $entries = Category::where('translation_lang', config('app.locale'))->where('parent_id','!=' ,0)->orderBy('lft')->get();

    if (is_null($entries)) {
        return [];
    }

    $tab = [];
    $tab[0] = 'Root';
    foreach ($entries as $entry) {
        if ($entry->id != $currentId) {
            $tab[$entry->translation_of] = '- ' . $entry->name;
        }
    }
    return $tab;
}

我想要subcategories()中所选选项的ID,我可以使用该ID来获取数据。

1 个答案:

答案 0 :(得分:1)

我认为最好的方法是为此特定目的创建自定义字段类型,其中包括两个选择。关注this procedure。从select2.blade.php文件开始,添加实现目标所需的javascript(在第一个select2上更改事件,更改下一个select2中的选项)。