【发布时间】:2018-04-10 19:21:48
【问题描述】:
我正在使用 Laravel 5.5 项目。
我有locations 表,其定义如下:
$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 和 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');
}
现在我需要绘制 ul li 树视图以从 location_id 为 null 开始 我这样做了
@foreach($locations->where('location_id','=','null') as $location)
@endforeach
这个循环开始第一个父位置 我需要的是 while 循环或 for 循环将所有子位置嵌套在第一个 foreach 循环中,并将孙子嵌套在子循环中作为 ul li 或类似这张照片的任何其他内容
谢谢
【问题讨论】:
-
向我们展示您目前拥有的代码 :)
标签: php laravel loops for-loop while-loop