我的数据库->带字段的表产品类别:
-身份证
-类别名称
-父ID级别
-您是否处于活动状态
要检索类别的我的产品实体:
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);
}
public function create()
{
$cats = $this->product->getAjaxGetCategoriesDropdown();
return View('products/create_product')->with(compact('products','cats'));
}
视图:(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>
<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>
日志显示的当前错误为:
create.blade.php中未定义的变量树
.. 代码
<?php foreach ($tree['first'] as $cat): ?>
我已经在一个Magento应用程序上成功地使用了这段代码,但是现在将同样的方法移植到Laravel并没有显示结果。谢谢你的帮助。