I am trying to do the following:
I have two tables:
1) Content
id,
section_id
parent_id,
sequence,
2) Sections
id,
title,
description,
date_entered
Each Content has to have a section, which is defined by a foreign key, the content can have a sub section, where if the content have the same parent_id - then this is classed as a sub section.. So for example:
1. My first section
1.1. My first sub section
2. My second section
3. My third section
3.1 My third sub section
I am using Eloquent and have used the following:
$sections = Content::orderBy('sequence', 'desc')
->groupBy('parent_id')->get();
If I output these within a foreach loop, then it will only show one of the records, where there are multiple that have the same parent_id, if I remove the groupBy
then it will display all the records, but not in groups
I have set up the relationship so that: there is a belongsTo
relationship.. So
public function sections()
{
return $this->belongsTo('App\Sections', 'section_id');
}
Where am I going wrong here?
UPDATE:
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