Php 同一模型上的雄辩父子关系
我有一个模型Php 同一模型上的雄辩父子关系,php,laravel,laravel-5,eloquent,laravel-5.1,Php,Laravel,Laravel 5,Eloquent,Laravel 5.1,我有一个模型课程模块,每个项目都与同一个模型相关 数据库结构: 模型中的关系: 我尝试了以下方法,但它只返回一个关系级别 尝试: 我试图创建一个json输出,如下所示 预期产出: 我不知道如何获取内部子对象。如果有未知深度,则必须递归获取子对象 另一种选择是使用嵌套集模型,而不是邻接列表模型。对于嵌套集,可以使用类似于baum/baumpackage for Laravel的东西 “嵌套集是实现有序树的一种智能方法,允许快速、非递归查询。”- 使用此包,您可以使用getsubstands等方法
课程模块
,每个项目都与同一个模型相关
数据库结构:
模型中的关系:
我尝试了以下方法,但它只返回一个关系级别
尝试:
我试图创建一个json输出,如下所示
预期产出:
我不知道如何获取内部子对象。如果有未知深度,则必须递归获取子对象 另一种选择是使用嵌套集模型,而不是邻接列表模型。对于嵌套集,可以使用类似于
baum/baum
package for Laravel的东西
“嵌套集是实现有序树的一种智能方法,允许快速、非递归查询。”-
使用此包,您可以使用getsubstands
等方法获取所有子级和嵌套子级,以及toHierarchy
获取完整的树层次结构
是可以帮助你的答案
我认为您必须递归地检索整个树:
$data = CourseModule::with('child_rec');
递归函数
这可能会根据您的要求帮助您
public function child()
{
return $this->hasMany('App\CourseModule', 'parent');
}
public function children_rec()
{
return $this->child()->with('children_rec');
// which is equivalent to:
// return $this->hasMany('App\CourseModule', 'parent')->with('children_rec);
}
// parent
public function parent()
{
return $this->belongsTo('App\CourseModule','parent');
}
// all ascendants
public function parent_rec()
{
return $this->parent()->with('parent_rec');
}
在children关系中,应该将与('children')
一起使用
在父关系中使用('parent')
对于要递归的代码:
public function parent()
{
return $this->belongsTo('App\CourseModule','parent_id')->where('parent_id',0)->with('parent');
}
public function children()
{
return $this->hasMany('App\CourseModule','parent_id')->with('children');
}
注意:确保您的代码具有某些或其他退出条件,否则它将以一个永无止境的循环结束。您始终可以创建自己的递归函数,在我的情况下,我按照下面的代码操作
<?php
declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Akmeh\Uuid;
/**
* Class Location
* @package Domain\Models
*/
class Location extends Model
{
use Uuid;
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
public $timestamps = false;
/**
* @param string $id
* @param array $tree
* @return array
*/
public static function getTree(string $id, array $tree = []): array
{
$lowestLevel = Location::where('id', $id)->first();
if (!$lowestLevel) {
return $tree;
}
$tree[] = $lowestLevel->toArray();
if ($lowestLevel->parent_id !== 0) {
$tree = Location::getTree($lowestLevel->parent_id, $tree);
}
return $tree;
}
}
模型功能:
public function Children()
{
return $this->hasMany(self::class, 'Parent', 'Id')->with('Children');
}
控制器功能:
Menu::with("Children")->where(["Parent" => 0])->get();
你的模特关系应该是这样的
// parent relation
public function parent(){
return $this->belongsTo(self::class , 'parent_id');
}
//child relation
public function children()
{
return $this->hasMany(self::class ,'parent_id');
}
public function ascendings()
{
$ascendings = collect();
$user = $this;
while($user->parent) {
$ascendings->push($user->parent);
if ($user->parent) {
$user = $user->parent;
}
}
return $ascendings;
}
public function descendings()
{
$descendings = collect();
$children = $this->children;
while ($children->count()) {
$child = $children->shift();
$descendings->push($child);
$children = $children->merge($child->children);
}
return $descendings;
}
您好,CourseModule::whereId($id)->first()->getDegenantsAndSelf()->toHierarchy()
只返回一个节点,如果我必须对模型进行任何更改,我将使用一个表,如topIt works中所示!在第一个集合中,我有一个链式树,但在相同的子集合中,子集合作为父集合重复。我不希望孩子们再次成为根集合。是否存在任何解决方案或我做错了?找到了解决方案$data=CourseModule::with('children')->where('parent_id',0)
我正在尝试将结果显示为层次结构,但无法。我试过这样的@foreach($accountHeads作为$accountHead)@foreach($accountHead->children作为$children){{{{$children->name}@endforeach@endforeach
。但它只显示那些具有parent\u id
=1
的。请您用更详细的解释来扩展您的答案好吗?这对理解非常有用。非常感谢。非常好的解决方案,谢谢
public function Children()
{
return $this->hasMany(self::class, 'Parent', 'Id')->with('Children');
}
Menu::with("Children")->where(["Parent" => 0])->get();
// parent relation
public function parent(){
return $this->belongsTo(self::class , 'parent_id');
}
//child relation
public function children()
{
return $this->hasMany(self::class ,'parent_id');
}
public function ascendings()
{
$ascendings = collect();
$user = $this;
while($user->parent) {
$ascendings->push($user->parent);
if ($user->parent) {
$user = $user->parent;
}
}
return $ascendings;
}
public function descendings()
{
$descendings = collect();
$children = $this->children;
while ($children->count()) {
$child = $children->shift();
$descendings->push($child);
$children = $children->merge($child->children);
}
return $descendings;
}