我尝试从数据库构建导航树,但我一直没有找到类错误。也创建了数据库表
class Navigation extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'navigation';
public function parent()
{
return $this->hasOne('navigation', 'id', 'parent_id');
}
public function children()
{
return $this->hasMany('navigation', 'parent_id', 'id');
}
public static function tree()
{
return static::with(implode('.', array_fill(0, 4, 'children')))->where('parent_id', '=', NULL)->get();
}
}
答案 0 :(得分:3)
假设您将默认的Laravel结构更改为'navigation'
到'App/Navigation'
class Navigation extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'navigation';
public function parent()
{
return $this->hasOne('App/Navigation', 'id', 'parent_id');
}
public function children()
{
return $this->hasMany('App/Navigation', 'parent_id', 'id');
}