【发布时间】: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