0

I am using Laravel 5.2 Backpack in my new project where I have a select_from_array field in my form, depending upon the selected value I want data to be displayed in another select_from_array field. Don't know how to do that. Please help me with this. This is my code

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;
}

I want the id of selected option in the subcategories() where I can use the id to get the data.

ekad
  • 14,436
  • 26
  • 44
  • 46
Pooja Krishna
  • 193
  • 1
  • 5
  • 26

1 Answers1

1

I think the best way for you is to create a custom field type for this particular purpose, that includes both selects. Follow this procedure. Start from the select2.blade.php file and add the javascript you need to achieve your goal (on change event on first select2, change the options in the next select2).

tabacitu
  • 6,047
  • 1
  • 23
  • 37
  • actually, in my databse both the category and subcategory are saved in the same table, and i don't know how to write relations. when i use select2 they ask for relation function..... please help me with this – Pooja Krishna Oct 05 '16 at 08:13
  • I understand. The select2 field won't work without relationships. Honestly, relationships are a must do in Laravel. Otherwise you probably shouldn't use Laravel at all. You can learn about relationships in the documentation here: https://laravel.com/docs/5.3/eloquent-relationships - but personally I'd recommend spending $9 for a month-access at Laracasts. You can learn Laravel really really easily by watching a few videos. This one in particular explains a lot of core concepts, as well as relationships (video 8): https://laracasts.com/series/laravel-5-from-scratch – tabacitu Oct 05 '16 at 17:48