0

select_from_arrayデータを別のフィールドに表示する選択した値に応じて、フォームにフィールドがある新しいプロジェクトで Laravel 5.2 バックパックを使用してい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が必要です。

4

1 に答える 1

1

この特定の目的のために、両方の選択を含むカスタム フィールド タイプを作成するのが最善の方法だと思います。この手順に従います。ファイルから開始し、select2.blade.php目標を達成するために必要な JavaScript を追加します (最初の select2 の変更イベントで、次の select2 のオプションを変更します)。

于 2016-09-23T16:00:46.017 に答える