Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 外键相同表LAVEL获取树视图_Php_Laravel_Loops_For Loop_While Loop - Fatal编程技术网

Php 外键相同表LAVEL获取树视图

Php 外键相同表LAVEL获取树视图,php,laravel,loops,for-loop,while-loop,Php,Laravel,Loops,For Loop,While Loop,我正在从事一个拉威尔5.5项目 我有位置表,定义如下: $table->increments('id'); $table->string('location_name',50); $table->integer('location_id')->unsigned(); $table->foreign('location_id')->references('id')->on('locations'); $table->softDel

我正在从事一个拉威尔5.5项目

我有
位置
表,定义如下:

$table->increments('id');
$table->string('location_name',50);
$table->integer('location_id')->unsigned();
$table->foreign('location_id')->references('id')->on('locations');          
$table->softDeletes();
$table->timestamps();
现在,主键和forgen键位于同一个表中 id和位置id 我在位置模型中有这种关系

public function get_sub()
{
    return $this->hasMany('App\Location','location_id','id');
}
public function get_father()
{
    return $this->belongsTo('App\Location','location_id','id');
}
现在,我需要绘制ulli树视图,以位置为开始。\u id为空 是我干的

@foreach($locations->where('location_id','=','null') as $location)

@endforeach
此循环从第一个父位置开始 我需要的是while-loop或for-loop将所有子对象位置嵌套在第一个foreach循环中,并将孙子对象作为ul-li或其他类似于此照片的对象嵌套在子对象循环中


谢谢,这只是有点混乱,但是很容易加载与之相关的第n级树。为此,我们可以创建一个递归函数来加载子关系或父关系

// Location model
// loads only 1st level children
public function children()
{
   return $this->hasMany(Location::class, 'parent_id', 'id');
}

// recursive, loads all children
public function childrenRecursive()
{
   return $this->children()->with('childrenRecursive');
}

// load 1st level parent
public function parent()
{
   return $this->belongsTo(Location::class,'parent_id', 'id');
}

// recursive load all parents.
public function parentRecursive()
{
   return $this->parent()->with('parentRecursive');
}

// here is how you can load the target tree structure.
$locations = Location::with('childrenRecursive')->whereNull('parent_id')->get();


//here is how you can create your menu tree.
function createMenuTree($locations) {
    echo "<ul>";
    foreach ($locations as $location) {

        if ($location->children->isEmpty() !== false) {
            echo "<li>" . $location->name;
            menu($location->children);
            echo "</li>";
        } else {
            echo "<li>" . $location->name . "</li>";
        }
    }
    echo "</ul>";
}
//位置模型
//仅加载第一级子级
公共职能儿童()
{
返回$this->hasMany(Location::class,'parent_id','id');
}
//递归,加载所有子项
公共函数childrenRecursive()
{
返回带有('childrenRecursive')的$this->children()->;
}
//加载第一级父级
公共函数父函数()
{
返回$this->belongsTo(Location::class,'parent_id','id');
}
//递归加载所有父对象。
公共函数parentRecursive()
{
返回$this->parent()->with('parentRecursive');
}
//下面是如何加载目标树结构。
$locations=Location::with('childrenRecursive')->whereNull('parent_id')->get();
//下面是创建菜单树的方法。
函数createMenuTree($locations){
回声“
    ”; foreach($locations作为$location){ 如果($location->children->isEmpty()!==false){ 回声“
  • ”$location->name; 菜单($location->children); 回声“
  • ”; }否则{ 回声“
  • ”$location->name.“
  • ”; } } 回声“
”; }
这只是有点混乱,但很容易加载与之相关的第n级树。为此,我们可以创建一个递归函数来加载子关系或父关系

// Location model
// loads only 1st level children
public function children()
{
   return $this->hasMany(Location::class, 'parent_id', 'id');
}

// recursive, loads all children
public function childrenRecursive()
{
   return $this->children()->with('childrenRecursive');
}

// load 1st level parent
public function parent()
{
   return $this->belongsTo(Location::class,'parent_id', 'id');
}

// recursive load all parents.
public function parentRecursive()
{
   return $this->parent()->with('parentRecursive');
}

// here is how you can load the target tree structure.
$locations = Location::with('childrenRecursive')->whereNull('parent_id')->get();


//here is how you can create your menu tree.
function createMenuTree($locations) {
    echo "<ul>";
    foreach ($locations as $location) {

        if ($location->children->isEmpty() !== false) {
            echo "<li>" . $location->name;
            menu($location->children);
            echo "</li>";
        } else {
            echo "<li>" . $location->name . "</li>";
        }
    }
    echo "</ul>";
}
//位置模型
//仅加载第一级子级
公共职能儿童()
{
返回$this->hasMany(Location::class,'parent_id','id');
}
//递归,加载所有子项
公共函数childrenRecursive()
{
返回带有('childrenRecursive')的$this->children()->;
}
//加载第一级父级
公共函数父函数()
{
返回$this->belongsTo(Location::class,'parent_id','id');
}
//递归加载所有父对象。
公共函数parentRecursive()
{
返回$this->parent()->with('parentRecursive');
}
//下面是如何加载目标树结构。
$locations=Location::with('childrenRecursive')->whereNull('parent_id')->get();
//下面是创建菜单树的方法。
函数createMenuTree($locations){
回声“
    ”; foreach($locations作为$location){ 如果($location->children->isEmpty()!==false){ 回声“
  • ”$location->name; 菜单($location->children); 回声“
  • ”; }否则{ 回声“
  • ”$location->name.“
  • ”; } } 回声“
”; }
“给我们展示你到目前为止拥有的代码:)给我们展示你到目前为止拥有的代码:)给我们展示你到目前为止拥有的代码::)((<代码>id,<代码>代码>代码>向我们展示你到目前为止拥有的代码::)你(代码)给我们展示你到目前为止你到目前为止拥有的代码:)((((代码>代码>id<代码>id
,<代码>代码>代码>地点地点你你你到目前为止,代码>我们我们我们你到目前你到目前为止的代码你有你到你到目前为止的代码你到目前为止的代码:::::::::::))给我们展示你到目前你到目前你到目前你到目前你到目前为止你到目前为止你到目前为止你到你到目前为止你到目前为止你到目前为止你到目前为止你到目前为止你到目前为止你到目前为止你到目前为止你到目前为止的代码你到目前为止你到目前为止2),(10),(2),(12),(51),(9),(13,'sdaasf',无效);我需要从这个值中画出嵌套的ulli,你可以创建一个函数并递归调用它来创建一些html并将其返回。菜单($location->children)应该是createMenuTree($location->children);(代码<代码>id<代码>id代码>id<代码>id<代码>id<代码>id),(1)1,(1,,,,,,,,,,,,,,,,[157555码码>码码码>id<代码>id码码码>id<<码码码码码>号号,,(1)1,(1,,,,,,(2,,,,,,,,,,,'5周五周五周五2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008年的各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各各1),(11),(2),(12),(51),(9),(13),(sdaasf),无效);我需要从这个值中画出嵌套的ulli,你可以创建一个函数并递归调用它来创建一些html并将其返回。菜单($location->children)应该是createMenuTree($location->children);