【问题标题】:multilevel menu in laravel with recursive functionlaravel中具有递归功能的多级菜单
【发布时间】:2018-08-28 05:40:58
【问题描述】:

我的项目在 Laravel 5.5 中 我有一张 wposts 表格,用于 laravel 中的多级菜单和同时发布的帖子。该表的字段为:

- id
- parentpost (id of parent post)
- title
- porder (order of posts in the same menu)

在控制器中,我具有 getwposts 功能,可以使用递归获取帖子,从而获得多级菜单。

private function getwposts($parentid = 0,$wposts = []){

    foreach(Wpost::where('parentpost', $parentid)->orderby('porder')->get() as $pwpost)
    {
        echo $pwpost->id.'-' ;
        $wposts[$pwpost->id] = $this->getwposts($pwpost->id,$wposts);
    }
return $wposts;    
}

在那之后,在同一个控制器中,我有一个名为 preview 的函数来呈现视图

public function preview($templ){

   $pwposts = \App\Wpost::with('parent')->get();

    $pwposts= $this->getwposts(0,[]);
    dd($pwposts);
    return view('templates.$templ1,compact('pwposts'));
}

我快到了。表格的莱特顺序是

但结果是

虽然使用 echo 我在树视图中看到了正确的记录顺序,但还是不行。 我的模型是

 namespace App;

 use Illuminate\Database\Eloquent\Model;

 class Wpost extends Model
 {
    protected $fillable = 
   ['parentpost','title','body','author','storeid','porder','haskids'];
   protected $table = 'wposts';

   public function children(){
       return $this->hasmany(Wpost::class, 'parentpost');
   }   

   public function parent(){
       return $this->belongsTo(Wpost::class, 'parentpost');
   }
 }

我被堆叠了。你能帮助我吗? 提前谢谢

【问题讨论】:

标签: php laravel recursion menu


【解决方案1】:

你可以试试https://github.com/etrepat/baum

我用过,感觉很好

【讨论】:

  • 这里面有很多理论,我没有时间去测试它。谢谢
【解决方案2】:

在你的模型中添加$with = ['children']

 namespace App;

 use Illuminate\Database\Eloquent\Model;

 class Wpost extends Model
 {
    protected $fillable = 
   ['parentpost','title','body','author','storeid','porder','haskids'];
   protected $table = 'wposts';
   protected $with = ['children'];



   public function children(){
       return $this->hasmany(Wpost::class, 'parentpost');
   }   

   public function parent(){
       return $this->belongsTo(Wpost::class, 'parentpost');
   }
 }

现在,如果您想以递归方式获取孩子,您可以使用。

   $pwposts = \App\Wpost::with('children')->get();

希望对你有帮助

【讨论】:

  • 我不明白添加的重点。我可以在语法中使用 -with- 来定义模型中的关系,但它不会改变任何东西。
  • ` $pwposts = \App\Wpost::with('children')->get(); ` 此行将递归返回您的表的所有数据
  • $with = ['children']; 将在每个请求中返回孩子
  • 我在getwposts函数中添加了-with-,变成了这样 foreach(Wpost::with('children')->where('parentpost', $parentid)->where('storeid' ,$id)->orderby('porder')->get() as $pwpost)
  • $with = ['children']; 你在你的模型中使用过这个吗?
【解决方案3】:

试试这个:

| id | parend_id | name     | url                 |

| 1  |     0     | Level 1  | /level_one          |

| 2  |     1     | Sublevel | /level_one/sublevel |

| 3  |     0     | Level 2  | /level_two          | 

型号

public function children() {
    return $this->hasMany('App\Menus', 'parent_id', 'id');
}

public static function tree() {
    return static::with(implode('.', array_fill(0, 100, 'children')))->where('parent_id', '=', '0')->get();
}

控制器

$components = new Component;
try {
     $menu = $components->tree();
} catch (Exception $e) {}
return response()->json($menu);

【讨论】:

    猜你喜欢
    • 2012-06-02
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 2014-01-03
    • 1970-01-01
    • 2023-04-11
    • 2018-07-08
    相关资源
    最近更新 更多