0

Im trying to create Nested Dropdown categories using the following code:

My DB-> Table product_categories with fields: -id -categoryName -parentId-level -is_active

My Product Entity to retrieve categories:

    public function getAjaxGetCategoriesDropdown() {
        $categories = DB::table('product_categories')
            ->where('is_active', 1)
            ->orderBy('path', 'asc')
            ->select('categoryName','level','id','parentId')
            ->get();

    $first = array();
    $children = array();
    foreach ($categories as $cat) {
        if ($cat->level == 2) {
            $first[$cat->id] = $cat;
        } else if ($cat->parentId) {
            $children[$cat->parentId][] = $cat;
        }
    }
    return array('first' => $first, 'children' => $children);
}

Controller:

public function create()
{
    $cats = $this->product->getAjaxGetCategoriesDropdown();
    return View('products/create_product')->with(compact('products','cats'));
}

In View: (create.blade.php)

<div class="section">
  <label class="field select">
        <select id="first_cat" class="required-entry validate-select" onchange="showCat(this, 2)">
      <option value=""> Select a Category </option>
        <?php foreach ($tree['first'] as $cat): ?>
      <option value="<?php echo $cat->id ?>"><?php echo $cat->categoryName ?></option>
<?php endforeach ?>
</select>
<i class="arrow double"></i>
</label>
<div id="sub_cat"></div>
</div>

My Script:

<script type="text/javascript">
    var children = $H(<?php echo json_encode($tree['children']) ?>);

    function showCat(obj, level) {
        var catId = obj.value;
        level += 1;
        if ($('cat_container_' + level)) {
            $('cat_container_' + level).remove();
        }
        if (children.get(catId)) {
            var options = children.get(catId);
            var html = '<select id="cat_' + catId + '" onchange="showCat(this, ' + level + ')">' + '<option value="">Select a SubCategory</option>';
            for (var i = 0; i < options.length; i++) {
                html += '<option value="' + options[i].entity_id + '">' + options[i].name + '</option>';
            }
            html += '</select>' + '<i class="arrow double"></i>';
            html = '<label class="field select ' + 'cat_container_' + level + '">' + html + '</label>';

            $('sub_cat').insert(html);
        }
    }
</script>

The current error that the Log displays says:

Undefined Variable Tree in create.blade.php

.. code <?php foreach ($tree['first'] as $cat): ?>

I've been using this code with success on a Magento app, but now porting the same approach to Laravel is not showing results. Any help appreciated.

s_h
  • 1,476
  • 2
  • 27
  • 61
  • 1
    Where in this code are you defining `$tree` and passing it to the view? – miken32 Sep 21 '18 at 19:59
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/q/4261133/1255289) – miken32 Sep 21 '18 at 20:00
  • hi @miken32 , I put that information at the script var children = $H(); – s_h Sep 21 '18 at 20:02

1 Answers1

1

Change

foreach($tree ... 

To

foreach($cats ...

In your view file.

wheelmaker
  • 2,975
  • 2
  • 21
  • 32
  • yes its perfect!. I will accept the answer as soon SO allows me. thanks! – s_h Sep 21 '18 at 20:06
  • Also you're trying to pass 'products' to you're view from the controller method but you're not defining it there. – wheelmaker Sep 21 '18 at 20:07
  • yes but that part is in model. My problem resides in your suggestion. – s_h Sep 21 '18 at 20:11
  • for some reason $('sub_cat').insert(html); is not inserting anything wrong. sorry to bother you, but can you see anything at script that could be wrong? thank you – s_h Sep 22 '18 at 02:21
  • 1
    try to console.log your html var before that line to make sure it's getting into that if statement and setting it the way you want – wheelmaker Sep 23 '18 at 19:58