<tfoot id='sdbHr'></tfoot>

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

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

      • <bdo id='sdbHr'></bdo><ul id='sdbHr'></ul>

        Laravel 获取祖先(URL)

        时间:2023-09-22

        • <bdo id='3RKmP'></bdo><ul id='3RKmP'></ul>
        • <tfoot id='3RKmP'></tfoot>

            <small id='3RKmP'></small><noframes id='3RKmP'>

              <tbody id='3RKmP'></tbody>
            <i id='3RKmP'><tr id='3RKmP'><dt id='3RKmP'><q id='3RKmP'><span id='3RKmP'><b id='3RKmP'><form id='3RKmP'><ins id='3RKmP'></ins><ul id='3RKmP'></ul><sub id='3RKmP'></sub></form><legend id='3RKmP'></legend><bdo id='3RKmP'><pre id='3RKmP'><center id='3RKmP'></center></pre></bdo></b><th id='3RKmP'></th></span></q></dt></tr></i><div id='3RKmP'><tfoot id='3RKmP'></tfoot><dl id='3RKmP'><fieldset id='3RKmP'></fieldset></dl></div>
            • <legend id='3RKmP'><style id='3RKmP'><dir id='3RKmP'><q id='3RKmP'></q></dir></style></legend>
                  本文介绍了Laravel 获取祖先(URL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在 Laravel 中,我有一个包含 id、parent_id、slug (Self-referring) 的表,

                  In Laravel, I have a table which contains id, parent_id, slug (Self-referring),

                  当我有一个ID时,我需要以这样的格式(以/"分隔)获取其所有祖先.

                  When I have an ID, I need to get all its ancestors in a format like this (Separated by "/").

                  level1/level2/level3
                  

                  但是在没有像laravel-nestedset"这样的包的情况下以一种有效的方式".

                  But in an efficient way without a package like "laravel-nestedset ".

                  我是这样实现的.

                  public function parent()
                  {
                      return $this->belongsTo('Collection', 'parent_id');
                  }
                  
                  public function getParentsAttribute()
                  {
                      $parents = collect([]);
                  
                      $parent = $this->parent;
                  
                      while(!is_null($parent)) {
                          $parents->push($parent);
                          $parent = $parent->parent;
                      }
                  
                      return $parents;
                  }
                  

                  还有其他方法可以有效地做到这一点并用/"分隔吗?

                  Any other way to do it efficiently and separated by "/" ?

                  推荐答案

                  在评论中进行了一些交谈后,我认为这是一个很好的解决方案:

                  After a little conversation in the comments I think this is a good solution:

                  // YourModel.php
                  
                  // Add this line of you want the "parents" property to be populated all the time.
                  protected $appends = ['parents'];
                  
                  public function getParentsAttribute()
                  {
                      $collection = collect([]);
                      $parent = $this->parent;
                      while($parent) {
                          $collection->push($parent);
                          $parent = $parent->parent;
                      }
                  
                      return $collection;
                  }
                  

                  然后您可以使用以下方法检索您的父母:

                  Then you can retrieve your parents using:

                  • YourModel::find(123)->parents(集合实例)
                  • YourModel::find(123)->parents->implode('yourprop', '/')(内爆为字符串,见 https://laravel.com/docs/5.4/collections#method-implode)
                  • YourModel::find(123)->parents->reverse()->implode('yourprop', '/')(倒序https://laravel.com/docs/5.4/collections#method-reverse)
                  • YourModel::find(123)->parents (collection instance)
                  • YourModel::find(123)->parents->implode('yourprop', '/') (imploded to string, see https://laravel.com/docs/5.4/collections#method-implode)
                  • YourModel::find(123)->parents->reverse()->implode('yourprop', '/') (reversed order https://laravel.com/docs/5.4/collections#method-reverse)

                  正如 Nikolai Kiselev https://stackoverflow.com/a/55103589/1346367 所指出的,您也可以将它组合起来这样可以节省一些查询:

                  As noted by Nikolai Kiselev https://stackoverflow.com/a/55103589/1346367 you may also combine it with this to save a few queries:

                  protected $with = ['parent.parent.parent'];
                  // or inline:
                  YourModel::find(123)->with(['parent.parent.parent']);
                  

                  这会在对象加载时预加载父级.如果您决定不使用它,则在您调用 $yourModel->parent 时(延迟)加载父项.

                  This preloads the parent on object load. If you decide not to use this, the parent is (lazy) loaded as soon as you call $yourModel->parent.

                  这篇关于Laravel 获取祖先(URL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel 中关系的计数关系 下一篇:使用 Eloquent ORM/laravel 准备好的语句

                  相关文章

                  最新文章

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

                    <bdo id='E2BeA'></bdo><ul id='E2BeA'></ul>
                    1. <tfoot id='E2BeA'></tfoot>

                      <small id='E2BeA'></small><noframes id='E2BeA'>

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