• <i id='8XcoH'><tr id='8XcoH'><dt id='8XcoH'><q id='8XcoH'><span id='8XcoH'><b id='8XcoH'><form id='8XcoH'><ins id='8XcoH'></ins><ul id='8XcoH'></ul><sub id='8XcoH'></sub></form><legend id='8XcoH'></legend><bdo id='8XcoH'><pre id='8XcoH'><center id='8XcoH'></center></pre></bdo></b><th id='8XcoH'></th></span></q></dt></tr></i><div id='8XcoH'><tfoot id='8XcoH'></tfoot><dl id='8XcoH'><fieldset id='8XcoH'></fieldset></dl></div>

    <tfoot id='8XcoH'></tfoot>

      • <bdo id='8XcoH'></bdo><ul id='8XcoH'></ul>

      <small id='8XcoH'></small><noframes id='8XcoH'>

      1. <legend id='8XcoH'><style id='8XcoH'><dir id='8XcoH'><q id='8XcoH'></q></dir></style></legend>

        如何使用 Laravel 创建数据库驱动的多级导航菜单

        How to create a database-driven multi-level navigation menu using Laravel(如何使用 Laravel 创建数据库驱动的多级导航菜单)
          <tbody id='e6lyz'></tbody>
          <bdo id='e6lyz'></bdo><ul id='e6lyz'></ul>

              <legend id='e6lyz'><style id='e6lyz'><dir id='e6lyz'><q id='e6lyz'></q></dir></style></legend>

              1. <small id='e6lyz'></small><noframes id='e6lyz'>

                • <tfoot id='e6lyz'></tfoot>
                  <i id='e6lyz'><tr id='e6lyz'><dt id='e6lyz'><q id='e6lyz'><span id='e6lyz'><b id='e6lyz'><form id='e6lyz'><ins id='e6lyz'></ins><ul id='e6lyz'></ul><sub id='e6lyz'></sub></form><legend id='e6lyz'></legend><bdo id='e6lyz'><pre id='e6lyz'><center id='e6lyz'></center></pre></bdo></b><th id='e6lyz'></th></span></q></dt></tr></i><div id='e6lyz'><tfoot id='e6lyz'></tfoot><dl id='e6lyz'><fieldset id='e6lyz'></fieldset></dl></div>
                  本文介绍了如何使用 Laravel 创建数据库驱动的多级导航菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 Laravel 4 的新手,我对它的模型完全感到困惑.我正在尝试为我的项目创建一个数据库驱动的导航菜单,我所知道的是我必须创建一个模型来与数据库交互(基于我从 codeigniter 获得的知识).我一直在学习,我厌倦了无法前进,这是我到现在想出的代码:

                  I'm new to Laravel 4 and I'm totally confused about it's models. I'm trying to create a database-driven navigation menu for my project and all I know is I have to create a model to interact with the database (based on my knowledge from codeigniter). I have been studying alot and I'm tired of not being able to go forward, this is the code I have come up with till now:

                  /app/models/navigation.php

                  <?php
                  
                  class Navigation extends Eloquent {
                  
                      /**
                       * The database table used by the model.
                       *
                       * @var string
                       */
                      protected $table = 'navigation';
                  
                      /**
                       * Get the unique identifier for the menu item.
                       *
                       * @return mixed
                       */
                      public function getItemIdentifier()
                      {
                          return $this->getKey();
                      }
                  
                  }
                  

                  这是我将用于此模型的导航数据库表:

                  And this is my Navigation database table I will use for this Model:

                  推荐答案

                  所以在从不同来源进行更多搜索和阅读之后,这就是我想出的,并且运行良好:

                  So after doing much more searching and reading from different sources this is what I came up with and it's working fine:

                  /app/models/Navigation.php

                  <?php
                  
                  class Navigation extends Eloquent {
                  
                      /**
                       * 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();
                  
                      }
                  
                  }
                  

                  /app/controllers/HomeController.php

                  <?php
                  
                  class HomeController extends BaseController {
                  
                      protected $layout = "layouts.main";
                  
                      public function showWelcome()
                      {
                  
                          $items = Navigation::tree();
                  
                          $this->layout->content = View::make('layouts.home.index')->withItems($items);
                  
                      }
                  
                  }
                  

                  /app/views/layouts/home/index.blade.php

                  <ul>
                      @foreach($items as $item)
                          <li>{{ $item->title }}
                              @foreach($item['children'] as $child)
                              <li>{{ $child->title }}</li>
                              @endforeach
                          </li>
                      @endforeach
                  </ul>
                  

                  这篇关于如何使用 Laravel 创建数据库驱动的多级导航菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                • <small id='srVuJ'></small><noframes id='srVuJ'>

                  1. <tfoot id='srVuJ'></tfoot>

                      <i id='srVuJ'><tr id='srVuJ'><dt id='srVuJ'><q id='srVuJ'><span id='srVuJ'><b id='srVuJ'><form id='srVuJ'><ins id='srVuJ'></ins><ul id='srVuJ'></ul><sub id='srVuJ'></sub></form><legend id='srVuJ'></legend><bdo id='srVuJ'><pre id='srVuJ'><center id='srVuJ'></center></pre></bdo></b><th id='srVuJ'></th></span></q></dt></tr></i><div id='srVuJ'><tfoot id='srVuJ'></tfoot><dl id='srVuJ'><fieldset id='srVuJ'></fieldset></dl></div>
                        • <bdo id='srVuJ'></bdo><ul id='srVuJ'></ul>
                            <tbody id='srVuJ'></tbody>

                        • <legend id='srVuJ'><style id='srVuJ'><dir id='srVuJ'><q id='srVuJ'></q></dir></style></legend>