Laravel 使用 ajax 请求在同一个 table 中返回 children of parents?
Laravel returning children of parents in same table using ajax request?
使用 Laravel 5.7 我有 table 个名字 territory_categories。在这个 table 我有 these Columns in the table
我如何使用与 ajax 请求的关系在同一个 table 中创建层次结构 parent 和 child?
You can write a simple self join for this scenario. In territory_categories
model write a relationship.
public function children(){
return $this->hasMany(territory_categories_model::class,'cat_parent_id', 'id');
}
Now from controller pass data to the view and write a foreach loop. You will be able to access the hierarchy structure.
$data = territory_categories_model::with('children')->get();
You will access the parent name also it's children now. For this case the parent will be repeating itself with each children
@foreach($data as $dt)
@if($dt->children->isNotEmpty())
@foreach($dt->children as $child)
<td>{{ $dt->territory_category_name }}</td>
<td>{{ $child->territory_category_name }}</td>
@endforeach
@endif
@endforeach
此代码运行良好,而不是上面一次
此代码适用于型号:
public function children() {
return $this->hasMany('Category','parent');
}
public function parent() {
return $this->belongsTo('Category','parent');
}
我在控制器中使用这样的检索:
public function store(Request $request){
if($request->territory_category_name){
$validator = $request->validate(['territory_category_name' => 'required', 'active' => 'required']);
$validator['cat_parent_id'] = $request->cat_parent_id;
TerritoryCategory::create($validator);
}
$territoryCategories = TerritoryCategory::with('parent')->get();
$data = [];
foreach ($territoryCategories as $key => $territoryCategory){
$key = ++$key;
$data[] = "
<tr>
<td>$key</td>
<td class='edit' data-url='/territoryCategory/update/$territoryCategory->id' data-id='$territoryCategory->id'>$territoryCategory->territory_category_name</td>
<td>
".(isset($territoryCategory->parent->territory_category_name) ? $territoryCategory->parent->territory_category_name : '<badge class="badge badge-info"> Parent </badge>')."
</td>
<td>
<input type='checkbox' name='active' ".($territoryCategory->active == 1 ? 'checked' : '')." disabled>
</td>
<td>
<div class='btn-group' role='group' aria-label='First group'>
<button data-method='PATCH' data-url='/territoryCategory/update/$territoryCategory->id' data-id='$territoryCategory->id' data-name='$territoryCategory->territory_category_name' data-active='$territoryCategory->active' type='button' class='btn btn-info territoryCategoryEdit' data-toggle='modal' data-target='#addTerritoryCategoryModal'><i class='la la-edit'></i></button>
<button data-url='/territoryCategory/delete/$territoryCategory->id' data-id='$territoryCategory->id' type='button' class='btn btn-danger territoryCategorydelete'><i class='la la-trash'></i></button>
</div>
</td>
</tr>
";
}
return response()->json(['success'=>'New Record added successfully!','territoryCategory' => $data]);
}
使用 Laravel 5.7 我有 table 个名字 territory_categories。在这个 table 我有 these Columns in the table 我如何使用与 ajax 请求的关系在同一个 table 中创建层次结构 parent 和 child?
You can write a simple self join for this scenario. In
territory_categories
model write a relationship.
public function children(){
return $this->hasMany(territory_categories_model::class,'cat_parent_id', 'id');
}
Now from controller pass data to the view and write a foreach loop. You will be able to access the hierarchy structure.
$data = territory_categories_model::with('children')->get();
You will access the parent name also it's children now. For this case the parent will be repeating itself with each children
@foreach($data as $dt)
@if($dt->children->isNotEmpty())
@foreach($dt->children as $child)
<td>{{ $dt->territory_category_name }}</td>
<td>{{ $child->territory_category_name }}</td>
@endforeach
@endif
@endforeach
此代码运行良好,而不是上面一次 此代码适用于型号:
public function children() {
return $this->hasMany('Category','parent');
}
public function parent() {
return $this->belongsTo('Category','parent');
}
我在控制器中使用这样的检索:
public function store(Request $request){
if($request->territory_category_name){
$validator = $request->validate(['territory_category_name' => 'required', 'active' => 'required']);
$validator['cat_parent_id'] = $request->cat_parent_id;
TerritoryCategory::create($validator);
}
$territoryCategories = TerritoryCategory::with('parent')->get();
$data = [];
foreach ($territoryCategories as $key => $territoryCategory){
$key = ++$key;
$data[] = "
<tr>
<td>$key</td>
<td class='edit' data-url='/territoryCategory/update/$territoryCategory->id' data-id='$territoryCategory->id'>$territoryCategory->territory_category_name</td>
<td>
".(isset($territoryCategory->parent->territory_category_name) ? $territoryCategory->parent->territory_category_name : '<badge class="badge badge-info"> Parent </badge>')."
</td>
<td>
<input type='checkbox' name='active' ".($territoryCategory->active == 1 ? 'checked' : '')." disabled>
</td>
<td>
<div class='btn-group' role='group' aria-label='First group'>
<button data-method='PATCH' data-url='/territoryCategory/update/$territoryCategory->id' data-id='$territoryCategory->id' data-name='$territoryCategory->territory_category_name' data-active='$territoryCategory->active' type='button' class='btn btn-info territoryCategoryEdit' data-toggle='modal' data-target='#addTerritoryCategoryModal'><i class='la la-edit'></i></button>
<button data-url='/territoryCategory/delete/$territoryCategory->id' data-id='$territoryCategory->id' type='button' class='btn btn-danger territoryCategorydelete'><i class='la la-trash'></i></button>
</div>
</td>
</tr>
";
}
return response()->json(['success'=>'New Record added successfully!','territoryCategory' => $data]);
}