Monday, March 20, 2017

Get all pages from Category and all sub categories

I'm trying to get all pages from category and all sub categories.

My current "Categories" table structure (NULL for parent category in "category_parent"):

id | category_name | category_parent

"Pages" table structure:

id | page_name

"PageRelations" table structure:

id | page_id | category_id

PageRelations model:

<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

class PageRelations extends Model
{
    public function categoryPages()
    {
        return $this->hasMany('App\Http\Models\Pages', 'id', 'page_id');
    }

    public function page()
    {
        return $this->hasOne('App\Http\Models\Pages', 'id', 'page_id');
    }
}

For example, if structure is:

+Cat1
  Page1
  Page2
  +Cat2
    Page3
    +Cat3
      Page4
      Page5
+Cat4
  Page6
  Page7

When I click on "Cat1" it returns: page1, page2, page3, page4, page5.

How to get all pages from category and all sub categories?



via rusbear28

Advertisement