Laravel使用ajax请求在同一表中返回父母的孩子?

问题描述 投票:-1回答:1

使用Laravel 5.7,我具有表名称region_categories。在此表中,我有these Columns in the table如何使用带有ajax请求的关系使同一层次结构中的父级和子级成为父级和子级?

php mysql ajax laravel relationship
1个回答
0
投票

您可以为这种情况编写一个简单的自我联接。在territory_categories模型中编写关系。

public function parent(){

  return $this->belongsTo(territory_categories_model::class,'cat_parent_id');

}

现在从控制器将数据传递到视图并编写一个foreach循环。您将能够访问层次结构。

$data = territory_categories_model::with('parent')->get();
© www.soinside.com 2019 - 2024. All rights reserved.