我正在研究Laravel。 我正在尝试在树视图中显示数据。但是我成功只是第一步。你们中有人可以帮忙吗?
DataBase Structure My achievement
代码:
function arraycopy(src, srcPos, dst, dstPos, length) {
return Object.assign(dst, Array(dstPos).concat(src.slice(srcPos, srcPos + length)))
}
console.log( arraycopy([2,3,4,5,6], 1, [1,1,1,1,1,1], 2, 3) )
答案 0 :(得分:0)
您需要对代码进行一些更改,请在下面的步骤中更改代码
第1步:请在模型文件中使用外键创建关系,例如
namespace App;
use Illuminate\Database\Eloquent\Model;
class Treeview extends Model
{
protected $table = 'treeview';
public $fillable = ['title','parent_id'];
/**
* Get the index name for the model.
*
* @return string
*/
public function childs() {
return $this->hasMany('App\Treeview','parent_id','id') ;
}
}
第2步:在控制器中添加以下功能,并根据您的表和模型对象进行一些更改
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function manageCategory()
{
$categories = Treeview::where('parent_id', '=', 0)->get();
return view('treeview/categoryTreeview',compact('categories')); // set the path of you templates file.
}
第3步:在您的categoryTreeview模板文件中添加以下代码
<h3>Category List</h3>
<ul id="tree1">
@foreach($categories as $category)
<li>
{{ $category->title }}
@if(count($category->childs))
@include('treeview/manageChild',['childs' => $category->childs]) // Please create another templates file
@endif
</li>
@endforeach
</ul>
第4步:请创建另一个模板文件manageChild,添加以下代码。
<ul>
@foreach($childs as $child)
<li>
{{ $child->title }}
@if(count($child->childs))
@include('treeview/manageChild',['childs' => $child->childs])
@endif
</li>
@endforeach
</ul>
输出显示为:
Category List
Cat_1
Cat_1_par_1
Cat_1_par_2
Cat_1_par_3
Cat_2
Cat_2_par_1
Cat_2_par_2
Cat_2_par_3
Cat_3
更多信息,我找到了一个链接:https://itsolutionstuff.com/post/laravel-5-category-treeview-hierarchical-structure-example-with-demoexample.html