【问题标题】:Laravel 5 - Elequent GROUP BY is failingLaravel 5 - Elequent GROUP BY 失败
【发布时间】:2015-10-11 21:50:07
【问题描述】:

我正在尝试执行以下操作:

我有两张桌子:

1) Content
   id, 
   section_id
   parent_id, 
   sequence,

2) Sections
   id,
   title, 
   description,
   date_entered

每个内容都必须有一个由外键定义的部分,内容可以有一个子部分,如果内容具有相同的 parent_id - 那么这被归类为一个子部分。例如:

 1. My first section
    1.1. My first sub section 
 2. My second section
 3. My third section
    3.1 My third sub section 

我正在使用 Eloquent 并使用了以下内容:

$sections = Content::orderBy('sequence', 'desc')
               ->groupBy('parent_id')->get();

如果我在 foreach 循环中输出这些,那么它只会显示其中一条记录,其中有多个具有相同 parent_id,如果我删除 groupBy 那么它将显示所有记录,但不在组

我已经建立了这样的关系:有一个belongsTo 关系.. 所以

  public function sections()
  {
     return $this->belongsTo('App\Sections', 'section_id');
  }

我哪里错了?

更新:

     1) Content
           id, 
           section_id
           parent_id, 
           sequence,

           FOREIGN KEYS:
           parent_id -> id,

           section_id -> id on Sections (below)

2) Sections
   id,
   title, 
   description,
   date_entered

【问题讨论】:

    标签: php mysql laravel-5 eloquent database-relations


    【解决方案1】:

    如果我理解正确,您希望获取 Content 对象及其子 Content 对象的列表,对吗?

    最简单的方法是在你的 Eloquent Content 模型中创建一个父子关系,然后使用它来加载带有孩子的父母:

    <?php
    class Content extends Model {
      public function children() {
        //this defines a relation one-to-many using parent_id field as the foreign key
        return $this->hasMany(Content::class, 'parent_id'); 
      }
    
      public function parent() {
        return $this->belongsTo(Content::class, 'parent_id'); 
      }
    
      public function section() {
        return $this->belongsTo(Section::class);
      }
    }
    

    然后,如果您想列出 Content 对象的 Section 以及它们的子项和部分,您可以像这样获取数据:

    $contents = Content::with(['children', 'section', 'children.section'])->whereNull('parent_id')->get();
    

    $contents 将包含所有没有父级的 Content 对象的集合。每个对象都有一个 $content->children 属性,该属性包含所有子 Content 对象的集合。所有子对象还将在 $childContent->parent 中保存对其父对象的引用。父母和孩子都会在 ->section 属性中拥有相应的部分。

    如果您现在想在 Blade 模板中显示一些内容层次结构,可以将 $contents 变量传递给视图并执行以下操作:

    <ul>
    @foreach($contents as $content)
      <li>{{$content->title}}</li>
      @if($content->children->count() > 0)
        <ul>
          @foreach($content->children as $childContent)
            <li>{{$childContent->title}}</li>
          @endforeach
       </ul>
      @endif
    @endforeach
    </ul>  
    

    我注意到您的模型中有一个 sequence 字段。我假设您希望按该字段对内容进行排序。在这种情况下,您需要修改获取数据的方式:

    $contents = Content::with(['children' => function($builder) {
      $builder->orderBy('sequence', 'desc');
    }, 'section', 'children.section'])->whereNull('parent_id')->get();
    

    【讨论】:

    • 谢谢你。遗憾的是,它什么也没返回。但我认为这是因为“内容”上的外键尚未设置。所以parent_id 必须是表内其他记录的外键?
    • 是的。对于孩子,您必须将其设置为父内容 ID 的 ID。对于父内容对象,它应该设置为 null。
    • 不,它不起作用.. 外键设置为:parent_id -> 用于内容表内的其他部分,section_id 外键用于部分表内的 id?
    • 你能用你如何定义外键来更新问题吗?我认为在呈现上述评论时格式会被破坏,我不确定你的意思。
    • 我已经更新了这个问题。我认为我做错了,因为parent_id 引用了同一张桌子上的id。但我认为它必须是@987654330 @ 作为parent_id 的外键,但是当我尝试添加此关系时,出现错误:150 - Foreign key constraint is incorrectly formed)
    猜你喜欢
    • 2016-01-15
    • 2015-10-25
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多