从类别和所有子类别中获取所有页面

时间:2017-03-20 10:56:29

标签: php laravel

我试图从类别和所有子类别中获取所有页面。

我目前的"类别"表结构(&#34中的父类别为NULL; category_parent"):

id | category_name | category_parent

"页"表结构:

id | page_name

" PageRelations"表结构:

id | page_id | category_id

PageRelations模型:

<?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');
    }
}

例如,如果结构是:

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

当我点击&#34; Cat1&#34;它返回:page1,page2,page3,page4,page5。

如何从类别和所有子类别中获取所有页面?

2 个答案:

答案 0 :(得分:0)

您必须更改一些路线:

Route::get('category/{id}','PagesController@displayProducts');

您的控制器:

class PagesController extends Controller {
    public function displayProducts($id) {
        $categories = Category::where('id', '=', $id)->get();
        $products = Product::where('cat_id','=', $id)->get();
        return view('category.show', compact('products', 'categories'));
    }
}

答案 1 :(得分:0)

控制器

<?php

namespace App\Http\Controllers;

use App\Models\Category;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    /**
     * @var Category
     */
    protected $category;

    /**
     * @var \Illuminate\Database\Eloquent\Builder
     */
    protected $query;

    /**
     * Controller constructor.
     * @param Category $category
     */
    public function __construct(Category $category)
    {
        $this->category = $category;
        $this->query = $this->category->newQuery();
    }

    public function home()
    {
        print_r($this->getAncestorCategoriesE1(2, 3)->toArray());
        print_r($this->getSubCategoriesE2(null, 7)->toArray());
        print_r($this->getSubCategoriesE2(1, 7)->toArray());
        print_r($this->getSubCategoriesE1(null, 8)->toArray());
        print_r($this->getSubCategoriesE1(1, 8)->toArray());
    }

    /**
     * Easy method but select all
     *
     * @param $categoryId
     * @param int $depth
     * @return \Illuminate\Database\Eloquent\Collection|string|static[]
     */
     public function getAncestorCategoriesE1($categoryId, $depth = 1) 
     {
         if (!is_numeric($depth) || $depth < 1) {
             return 'INvalid depth parameter';
         }

        $parents = [];

        for ($i = 0; $i < $depth; $i++) {
            $parents[] = 'parent';
        }

        $rel = implode('.', $parents);

        return $this->category->with($rel)->find($categoryId);
    }

    /**
     * Easy method but select all
     *
     * @param null $categoryId
     * @param int $depth
     * @return \Illuminate\Database\Eloquent\Collection|string|static[]
     */
    public function getSubCategoriesE1($categoryId = null, $depth = 1) {
        if (!is_numeric($depth) || $depth < 1) {
            return 'INvalid depth parameter';
        }

        $children = [];
        for ($i = 0; $i < $depth; $i++) {
            $children[] = 'children';
        }

        $rel = implode('.', $children);
        $this->addCategoryCondition($categoryId);

        return $this->category->with($rel)->get();
    }

    /**
     * @param null $categoryId
     * @param int $depth
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getSubCategoriesE2($categoryId = null, $depth = 4)
    {
        $this->addCategoryCondition($categoryId);
        $this->pushSelectInQuery($this->query);
        $this->pushWithInQuery($this->query, $depth);
        return $this->query->get();
    }

    /**
     * @param $query
     */
    public function pushSelectInQuery($query)
    {
        $query->select('name', 'id', 'parent_id');
    }

    /**
     * @param $query
     * @param int $depth
     */
    public function pushWithInQuery($query, $depth = 1)
    {
        $query->with(['children' => function($query) use ($depth) {
            $this->pushSelectInQuery($query);
            if (1 != $depth) {
                $this->pushWithInQuery($query, --$depth);
            }
        }]);
    }

    /**
     * @param $categoryId
     */
    public function addCategoryCondition($categoryId)
    {
        if (is_null($categoryId)) {
            $this->query->whereNull('parent_id');
        } else {
            $this->query->where('parent_id', $categoryId);
        }
    }
}

模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'parent_id', 'name'
    ];

    /**
     *
     */
    public function children()
    {
        return $this->hasMany(Category::class,  'parent_id', 'id');
    }

    /**
     *
     */
    public function parent()
    {
        return $this->belongsTo(Category::class, 'parent_id', 'id');
    }
}

在控制器中有DRY我稍后修复!!!请参阅此解决方案Getting sub category info in parent category in laravel