Laravel和数据库的Treeview

时间:2018-08-28 17:43:51

标签: laravel treeview

我在Internet上发现了此功能,并且可以正常工作。

在我的应用中

public function childs(){
    return $this->hasMany('App\Account','p_id');
}

在我的路线上:

Route::get('tests', function(){
return App\Account::with('childs')
->where('p_id',0)
->get();
});

因此,如果您在数据库中包含p_id = 0id = 1的原始文件,它将返回一个类别,如果您拥有p_id = 1id = 2的原始文件,则意味着id = 1类别的子级。

另一个例子: 如果您有p_id = 6id = 16的原始字符,则意味着该类别的子级带有id = 6

请谁能解释一下,为什么它确实能那​​样工作?

1 个答案:

答案 0 :(得分:0)

Laravel Relationship不用于在1个模型之间建立关系。您无法在AccountAccount之间建立联系。一个帐户没有“ hasMany”帐户。

例如,您可以拥有hasMany用户。

public function childs(){
   return $this->hasMany('App\User','p_id');
}