0
我有如下表:的foreach UL李如无边条件
public function up()
{
Schema::create('locations', function (Blueprint $table) {
$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();
});
}
,我想它看起来就像一棵树,但仍然可以使用它在我的UL李下拉列表。 在我的刀,我做到了这一点:
<ol>
@foreach($locations->where('location_id','=',null) as $location)
<li draggable='true'>
{{$location->location_name}}
<ol>
@foreach($location->get_sub as $location_sub)
<li draggable='true'>{{$location_sub->location_name}}</li>
@endforeach
</ol>
</li>
@endforeach
</ol>
$位置 - > get_sub是函数模型 内部的关系
public function get_sub()
{
return $this->hasMany('App\Location','location_id','id');
}
这样我能达到的父/子位置;但是,有时我需要元素父母的孩子的曾孙,孙等 我的想法做到这一点是做一个while循环来寻找每个孩子的位置和 ,并把它们放在ul列表和每个get_sub显示它下它与父UL李里面UL李书福看起来像这样:
虽然有时位置得到7个父亲,如果我知道父亲的号码,我可以让while循环多达父亲电话号码,但编译时我不知道这个数字。
$ locations = Location :: withTrashed() - > get(); \t \t return view('locations.create',compact('locations'));这是我的控制器代码我已经做了你所说的 –
我的问题是我希望它从最后一个孩子不是从父母开始 –